在解决内部承诺之前,承诺链会继续
Promise chain continues before inner promise is 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);
});
但问题是,即使我 return 新的承诺,在解决任何内部承诺之前,执行会继续沿着链进行。怎么会?是不是应该等到异步请求成功,在success
回调中调用resolve()
?
你没有做错任何事。这是jQuery的错,as so often1.
问题是 jQuery 不 Promises/A+ 兼容(直到 v 3.0),并且无法从除它自己的实现之外的其他实现中采用 promises/thenable。因此,当您的回调执行 return RSVP 承诺时,jQuery 只是将它们视为要实现的值,而不是等待它们。
您 可以 将所有承诺投射到 jQuery 延期并且它会起作用,但您真的不希望那样。要使标准 Promise
行为(由 RSVP 提供)起作用,您需要避免 jQuery 的错误 then
。这个:
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)
…
与
每次我认为我理解了承诺,我证明自己错了!
我有这样写的函数
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);
});
但问题是,即使我 return 新的承诺,在解决任何内部承诺之前,执行会继续沿着链进行。怎么会?是不是应该等到异步请求成功,在success
回调中调用resolve()
?
你没有做错任何事。这是jQuery的错,as so often1.
问题是 jQuery 不 Promises/A+ 兼容(直到 v 3.0),并且无法从除它自己的实现之外的其他实现中采用 promises/thenable。因此,当您的回调执行 return RSVP 承诺时,jQuery 只是将它们视为要实现的值,而不是等待它们。
您 可以 将所有承诺投射到 jQuery 延期并且它会起作用,但您真的不希望那样。要使标准 Promise
行为(由 RSVP 提供)起作用,您需要避免 jQuery 的错误 then
。这个
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)
…