Going to address some of GiGs' questions first: GiGs said: I havent done anything with promises, so cant help you there. But I'm wondering why you'd need to use them. If the table or handouts exist, you dont need to use promises to access them. To access the bio/info/gm notes of handouts/characters, you need to do an asynchronous callback to actually decode the content of the section, as shown in Aku's sample code. Because it's asynchronous, you can either wrap all your code in successive asynchronous callbacks (if you need to access multiple handouts); this is what I've heard referred to (appropriately) as callback hell. Using a promise allows the code to avoid the true depths of depravity of recursively nested asynchronous callbacks. Even better though is to combine a promise with the async/await capability of ES7. Now, for Aku's code question I have done several scripts using promises and/or async/await. So, your problem is two fold Aku. You define a promise a little differently than you have it; new Promise instead of just promise . Additionally, your method of resolving the promise won't work, but you haven't run into that yet because of the reference error. To resolve a promise, the resolution has to occur in the asynchronous part, not outside of it. Here's how the promise definition and resolution/rejection should go: on("ready", function () {
'use strict';
let customTimesHandout = findObjs({_type:"handout",name:"Custom Times"})[0],//this allows you to skip the need for that if(customTimesHandout &&...). findObjs always returns an array, if there were no results, then getting index 0 of the array will give null.
ReadFiles = new P romise(function(resolve,reject){
if(customTimesHandout){
customTimesHandout.get("notes",function(notes){
log("in the callback notes is :" + notes);
resolve(notes);
});
}else{
log("Did not find the handout")
var customTimes = false
reject(false);
}
});
ReadFiles.then(function(result){
log(result)
}, function(err){
log(err);
}); Also, you could use async/await, to not have to enter the minor callback hell of using .then: on("ready", async function () {//denotes this as an asynchronous function that will pause during it's execution
'use strict';
let customTimesHandout = findObjs({_type:"handout",name:"Custom Times"})[0],//this allows you to skip the need for that if(customTimesHandout &&...). findObjs always returns an array, if there were no results, then getting index 0 of the array will give null.
ReadFiles = await new Promise(function(resolve,reject){//the await tells the script to pause here and wait for this value to appear. Once a value is returned, the script will continue on its way
if(customTimesHandout){
customTimesHandout.get("notes",function(notes){
log("in the callback notes is :" + notes);
resolve(notes);//resolving the promise gives a value that unpauses the script execution
});
}else{
log("Did not find the handout")
var customTimes = false
reject(false);//reject also gives a value to the promise to allow the script to continue
}
});
log(`readFiles:${readFiles}`);
}); Note, that you may need to switch to something like a revealing module pattern to use the async await, but if I remember correctly the async declaration should work just fine in the on() callback function declaration. EDIT: the MDN page on promises has some good information. I'll also see if I can dig up the intros and guides to promises and async/await I read when I was diving into this stuff.