What does `tp.file.exists` return?

What does tp.file.exists return?

I am trying to create a custom javascript function. Whenever I return tp.file.existsit returns trueor false depending if the file exists or not but when I try to use it in a condition it always returns false. What is going on?

return tp.file.exists("blahblahblah") == false;

blahblahblah doesn’t exist so it should be false == false which should return true but it still returns false.

Manually typing false == false does actually return true.

Exists is a promise, so you need to await it:

https://silentvoid13.github.io/Templater/internal-functions/internal-modules/file-module.html#examples

You’re checking if “promise == false” , and of course it does not.

return (await tp.file.exists("blahblahblah")) === false;

That was part of the answer. The other part that I wasn’t aware of until I found out how to open the console was that javascript functions which use await need to be declared asynchronous. @AlanG, thanks for pointing me in the right direction. I’m still very new to Obsidian and javascript.

My question was a way to deal with daily notes in a way logical to me and at the same time learn how to do my own custom templater scripts. In my case my daily notes are in, unsurprisingly, Daily Notes/ and have the format YYYY.MM.DD - dddd. I wanted to add a link to the previous daily note, even if I hadn’t made a daily note in more than a day.

Here is the working code.

Templater Template

[[<% tp.user.testJS(tp,"YYYY.MM.DD - dddd","Daily Notes") %>]]

testJS.js

async function testJS(tp,timeFMT,dailyFolder) {
	let i =0;  // Current iteration
	let iMax = -100;  // Max iterations
	let fileFound = false;
	do {
		i--;
		fileFound = await tp.file.exists(dailyFolder + "/" + tp.date.now(timeFMT, i) + ".md");
	}
	while (fileFound == false & i > iMax);
	if (fileFound) {
		return tp.date.now(timeFMT, i);
	} else {
		return "";
	}
}
module.exports = testJS;

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.