Promise 链在内部 Promise 被解决之前继续

IT技术 javascript promise rsvp.js
2021-02-20 15:17:38

Promise 在内部Promise解决之前解决类似的问题,但我仍然无法让它工作。

每次我认为我理解了Promise,我就证明自己错了!

我有这样写的函数

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);
            });

但问题是,即使我返回新的Promise,在解决任何内部Promise之前,执行仍会继续沿链向下。怎么来的?不应该等到异步请求成功并resolve()success回调中调用吗?

1个回答

你在这里没有做错任何事。这是 jQuery 的错,就像1 一样

问题是 jQuery 不兼容Promises/A+(直到 v 3.0),并且无法从其他实现而不是它自己的实现中采用 promises/thenable。因此,当您的回调确实返回 RSVP Promise时,jQuery 只是将它们视为要实现的值,而不是等待它们。
可以将所有Promise都转换为 jQuery deferred 并且它会起作用,但您真的不想要那样。要使标准Promise行为(由 RSVP 提供)起作用,您需要避免 jQuery 的 buggy 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)
    …
1):这也发生在这里与本地Promise和这里与 RSVP
2021-05-04 15:17:38
Geebus,我从没想过可能是因为这个,但现在确实有效!非常感谢!
2021-05-10 15:17:38