Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue with loadFile #332

Open
RANKKY-YT opened this issue Dec 10, 2019 · 2 comments
Open

Issue with loadFile #332

RANKKY-YT opened this issue Dec 10, 2019 · 2 comments

Comments

@RANKKY-YT
Copy link

After the 2.0 update I still get a "DEPRECATED: RiveScript.loadFile() now returns a Promise instead of using callbacks" error eventhough I fixed my code...

let bot = new RiveScript();
bot.loadFile("/brain.rive").then(function() {
console.log("Bot loaded!");
bot.sortReplies();
}).catch(function(err, filename, lineno) {
console.error("An error occurred!");
});

@kirsle
Copy link
Member

kirsle commented Nov 26, 2020

Does anyone else get an error like this? According to the rivescript source and your example code, it shouldn't be triggering this warning:

/**
async loadFile(string path || array path)
Load a RiveScript document from a file. The path can either be a string that
contains the path to a single file, or an array of paths to load multiple
files. The Promise resolves when all of the files have been parsed and
loaded. The Promise rejects on error.
This loading method is asynchronous so you must resolve the promise or
await it before you go on to sort the replies.
For backwards compatibility, this function can take callbacks instead
of returning a Promise:
> `rs.loadDirectory(path, onSuccess(), onError(err, filename, lineno))`
* resolves: `()`
* rejects: `(string error)`
*/
async loadFile(path, onSuccess, onError) {
var self = this;
// Did they give us a single path?
if (typeof path === "string") {
path = [path];
}
let promises = new Array();
for (let i = 0, len = path.length; i < len; i++) {
let file = path[i];
self.say(`Request to load file: ${file}`);
promises.push(function(f) {
// This function returns a Promise. How are we going to load
// the file?
if (self._runtime === "web") {
// Via ajax!
return self._ajaxLoadFile(f);
} else {
// With node fs module!
return self._nodeLoadFile(f);
}
}(file));
}
// The final Promise to return.
let promise = new Promise((resolve, reject) => {
Promise.all(promises).then(resolve).catch(reject);
});
// Legacy callback style?
if (typeof(onSuccess) === "function") {
self.warn("DEPRECATED: RiveScript.loadFile() now returns a Promise instead of using callbacks")
return promise.then(onSuccess).catch(function(err, filename, lineno) {
if (typeof(onError) === "function") {
onError.call(null, err, filename, lineno);
}
});
} else {
return promise;
}
}

It checks if the onSuccess parameter (second param after the file names) is of type "function", if you were just calling loadFile("brain.rive") the param ought to be null, unless maybe some part of your codebase is still calling the function using the older syntax?

@telnetd4f
Copy link

I had this issue when i ran outdated nodejs along side the web pack back in 2019? couldn't tell you what kept displaying it but it was related to the 2019 source code that's a given after 2020 this never happened again at lease this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants