与Promise resolve before inner promise resolved类似的问题,但我无法让它继续工作。
每次我认为我理解承诺,我证明自己错了!
我有像这样写的函数
function getFileBinaryData () {
var promise = new RSVP.Promise(function(resolve, reject){
var executorBody = {
url: rootSite + sourceRelativeUrl + "/_api/web/GetFileByServerRelativeUrl('" + fileUrl + "')/$value",
method: "GET",
binaryStringResponseBody: true,
success: function (fileData) {
resolve(fileData.body);
},
error: function (argument) {
alert("could not get file binary body")
}
}
sourceExecutor.executeAsync(executorBody);
});
return promise;
}
function copyFileAction (fileBinaryData) {
var promise = new RSVP.Promise(function(resolve, reject){
var executorBody = {
url: rootSite + targetWebRelativeUrl + "/_api/web/GetFolderByServerRelativeUrl('" + targetList + "')/Files/Add(url='" + fileName + "." + fileExt + "', overwrite=true)",
method: "POST",
headers: {
"Accept": "application/json; odata=verbose"
},
contentType: "application/json;odata=verbose",
binaryStringRequestBody: true,
body: fileBinaryData,
success: function (copyFileData) {
resolve();
},
error: function (sender, args) {
}
}
targetExecutor.executeAsync(executorBody);
});
return promise;
}
我尝试像这样链接
$.getScript("/_layouts/15/SP.RequestExecutor.js")
.then(patchRequestExecutor)
.then(function(){
sourceExecutor = new SP.RequestExecutor(sourceFullUrl);
targetExecutor = new SP.RequestExecutor(targetList);
})
.then(getFileInformation)
.then(getFileBinaryData)
.then(copyFileAction)
.then(getTargetListItem)
.then(updateTargetListItem)
.catch(function (sender, args) {
});
或者像这样
$.getScript("/_layouts/15/SP.RequestExecutor.js")
.then(patchRequestExecutor)
.then(function(){
sourceExecutor = new SP.RequestExecutor(sourceFullUrl);
targetExecutor = new SP.RequestExecutor(targetList);
})
.then(function(){
return getFileInformation();
})
.then(function(){
return getFileBinaryData();
})
.then(function(binaryData){
return copyFileAction(binaryData)
})
.then(function(){
return getTargetListItem();
})
.then(function(listItem){
return updateTargetListItem(listItem);
});
问题是,即使我返回新的承诺,在任何内部承诺得到解决之前,执行仍在链中继续。怎么会?它不应该等到异步请求成功并在resolve()
回调中调用success
吗?
答案 0 :(得分:3)
你这里没有做错任何事。这是jQuery的错,as so often 1 。
问题是jQuery不是Promises/A+兼容的(直到v 3.0),并且无法从其他实现中采用promises / thenable而不是它自己的。因此,当您的回调确实返回RSVP承诺时,jQuery只会将它们视为要实现的值,而不是等待它们。
你可以将所有的承诺投射到延迟的jQuery,它会起作用,但你真的不希望这样。要使标准Promise
行为(由RSVP提供)起作用,您将需要避免jQuery的错误then
。这can easily be done:
RSVP.Promise.resolve($.getScript("/_layouts/15/SP.RequestExecutor.js"))
// ^^^^^^^^^^^^^^^
.then(patchRequestExecutor)
.then(function(){
sourceExecutor = new SP.RequestExecutor(sourceFullUrl);
targetExecutor = new SP.RequestExecutor(targetList);
})
.then(getFileInformation)
.then(getFileBinaryData)
.then(copyFileAction)
…