我试图在全局变量getMovie()
中获取movie_data
promise的值,但是值不在promise之外更新,它在promise中正常工作但是当我尝试打印值时在承诺解析器中movie_data
它仍然是空的。而且,解决&拒绝两者都被称为这是我没有得到的。
Output:
{"_id":"Movie1"} // inside actual promise
{} // print inside promise resolver
dummy text to check // print inside promise reject
Expected Output:
{"_id":"Movie1"} // inside promise
{"_id":"Movie1"} // inside promise resolver
我的代码出了什么问题?
var Promise = require('promise');
var movie_data = {};
function getMovie(movie_id) {
return new Promise(function(resolve,reject) {
callToMySQLProcedure('GET_FILM_DETAILS', [movie_id], function(err, data) { // call to my stoored procedure and getting response from db
data.forEach(function(elem) {
movie_data["_id"] = "Movie1";
console.log(movie_data);
resolve(movie_id);
});
});
});
}
getMovie(1)
.then(
console.log(movie_data),
console.log('dummy text to check')
)
.catch(function(e) {
console.log(e);
})