strongloop promise 何时执行数据库调用?
When does a strongloop promise execute the database call?
我正在重构别人的代码。
let promiseObj = Application.models.Widget.findById(connection.childId)
if (connection.child != 'Widget') {
typeES = "media"
promiseObj = Application.models.Media.findById(connection.childId)
}
promiseObj.then((obj) => {
let ownerId = obj.ownerId
let promiseUser = Application.models.MyUser.findById(ownerId)
})
问题是,服务器在什么时候被调用
"let promiseObj = Application.models.Widget.findById(connection.childId)" 已声明。
或者当 .then 被声明为 promise 有办法实现时,服务器是否被调用。
这是 ES6 的环回。
谢谢guys/girls:D
正如 Erazihel 所解释的那样,声明不会触发解析,而是在您调用 .then() 时调用它
我做了一个简单的例子,以可视化效果,在那里你会检查解析的时间是否与你调用 .then() 的时间相匹配,而不是在声明时。
p1.then(
// Then calls the resolve so the execution time 'resolve' matches .then
function(val) {
var timeMeAgain = new Date().toLocaleString();
log.insertAdjacentHTML('beforeend', val.thisPromiseCount +
') Promise done, then is called here (<small>Async call finished, .then called at: '+timeMeAgain+', promise executed at": '+val.timeCalledAt+' </small>)<br/>');
})
*代码基于 MDN 示例。检查文档 here
编辑
关于 resolve 方法,正如 MDN 文档所解释的:
The method returns a Promise object that is resolved with the given value.
If the value is a thenable (i.e. has a "then" method), the returned
promise will "follow" that thenable, adopting its eventual state;
otherwise the returned promise will be fulfilled with the value.
意思是当数据库或端点返回响应对象时会返回returns信息,不是在promise的声明上。
Does the server get called when let promiseObj = Application.models.Widget.findById(connection.childId)
is declared.
是的,一调用findById
方法就发起服务器请求。
Or does the server get called when the .then is declared as the promise have a way to be fulfilled.
then
只是一个被调用的方法,这里没有声明。它不会 "give the promise a way to be fulfilled" - 承诺将 always 在请求完成时解析(成功时执行,错误时拒绝),无论是否有任何回调与否。
如果您通过 then
安装回调,它将在承诺完成时被调用。
所以是的,我很确定你应该重构这段代码:
let promiseObj;
if (connection.child != 'Widget') {
promiseObj = Application.models.Widget.findById(connection.childId);
} else {
typeES = "media"
promiseObj = Application.models.Media.findById(connection.childId)
}
let promiseUser = promiseObj.then((obj) => {
return Application.models.MyUser.findById(obj.ownerId);
});
我正在重构别人的代码。
let promiseObj = Application.models.Widget.findById(connection.childId)
if (connection.child != 'Widget') {
typeES = "media"
promiseObj = Application.models.Media.findById(connection.childId)
}
promiseObj.then((obj) => {
let ownerId = obj.ownerId
let promiseUser = Application.models.MyUser.findById(ownerId)
})
问题是,服务器在什么时候被调用 "let promiseObj = Application.models.Widget.findById(connection.childId)" 已声明。
或者当 .then 被声明为 promise 有办法实现时,服务器是否被调用。
这是 ES6 的环回。
谢谢guys/girls:D
正如 Erazihel 所解释的那样,声明不会触发解析,而是在您调用 .then() 时调用它
我做了一个简单的例子,以可视化效果,在那里你会检查解析的时间是否与你调用 .then() 的时间相匹配,而不是在声明时。
p1.then(
// Then calls the resolve so the execution time 'resolve' matches .then
function(val) {
var timeMeAgain = new Date().toLocaleString();
log.insertAdjacentHTML('beforeend', val.thisPromiseCount +
') Promise done, then is called here (<small>Async call finished, .then called at: '+timeMeAgain+', promise executed at": '+val.timeCalledAt+' </small>)<br/>');
})
*代码基于 MDN 示例。检查文档 here
编辑
关于 resolve 方法,正如 MDN 文档所解释的:
The method returns a Promise object that is resolved with the given value. If the value is a thenable (i.e. has a "then" method), the returned promise will "follow" that thenable, adopting its eventual state; otherwise the returned promise will be fulfilled with the value.
意思是当数据库或端点返回响应对象时会返回returns信息,不是在promise的声明上。
Does the server get called when
let promiseObj = Application.models.Widget.findById(connection.childId)
is declared.
是的,一调用findById
方法就发起服务器请求。
Or does the server get called when the .then is declared as the promise have a way to be fulfilled.
then
只是一个被调用的方法,这里没有声明。它不会 "give the promise a way to be fulfilled" - 承诺将 always 在请求完成时解析(成功时执行,错误时拒绝),无论是否有任何回调与否。
如果您通过 then
安装回调,它将在承诺完成时被调用。
所以是的,我很确定你应该重构这段代码:
let promiseObj;
if (connection.child != 'Widget') {
promiseObj = Application.models.Widget.findById(connection.childId);
} else {
typeES = "media"
promiseObj = Application.models.Media.findById(connection.childId)
}
let promiseUser = promiseObj.then((obj) => {
return Application.models.MyUser.findById(obj.ownerId);
});