Reputation: 795
I use the following code and I got the warning
Warning: a promise was created in a handler but was not returned from it
var Promise = require("bluebird");
var glob = Promise.promisify(require('glob'));
glob("files/*.js")
.then(function (files) {
files.forEach(function (file) {
...
invoke(data,response,fn,entity,req);
});
}).catch(function (err) {
console.log('error: ', err);
}
)
what should I do to avoid it?
I read the documentation of BB and it seems that I should return
the promise
my question is where? I guess that inside the invoke function but not sure how
This is the inovke function
var invoke = function (data,res,fn,entity,req) {
....
for (var data in entity.app[0]) {
var name = entity.app[0].function;
try {
fn[name](req, res);
}
catch (err) {
res.status(500).send("Error" + err);
}
if (!res.headerSent) {
res.end("done", 200);
}
}
}
}
};
in addition I've tried to return the promise like following which doesnt work
var invoke = function (data,res,fn,entity,req) {
....
return new Promise(function (resolve, reject) {
for (var data in entity.app[0]) {
var name = entity.app[0].function;
try {
resolve(fn[name](req, res));
}
catch (err) {
res.status(500).send("Error" + err);
}
if (!res.headerSent) {
res.end("done", 200);
}
}
}
}
};
I dont want to suppress the warning I want to understand how to solve it...
If I need to add some additional info please let me know, Im stuck here:(
Upvotes: 2
Views: 174
Reputation: 689
I didn't test the following code but you should have an approach like this:
var Promise = require("bluebird");
var glob = Promise.promisify(require('glob'));
glob("files/*.js").then(function (files) {
return Promise.all(files.map(function (file) {
return invoke(data,response,fn,entity,req);
})
);
}).catch(function (err) {
console.log('error: ', err);
res.status(500).send("Error" + err);
}).finally(function(){
if (!res.headerSent) {
res.end("done", 200);
}
});
var invoke = function (data,res,fn,entity,req) {
return new Promise(function (resolve, reject) {
for (var data in entity.app[0]) {
var name = entity.app[0].function;
try {
fn[name](req, res);
}
catch (err) {
reject(err);
}
}
}
};
Upvotes: 1