【发布时间】:2020-06-05 20:45:30
【问题描述】:
我有三个基于 Promise 的函数,但我需要在中间运行一个非 Promise 函数。 例如:
function getListItems(listTitle) {
var deferred = $.Deferred();
var ctx = SP.ClientContext.get_current();
var list = ctx.get_web().get_lists().getByTitle(listTitle);
var items = list.getItems(SP.CamlQuery.createAllItemsQuery());
ctx.load(items);
ctx.executeQueryAsync(
function () {
deferred.resolve(items);
},
function (sender, args) {
deferred.reject(sender, args);
}
);
return deferred.promise();
}
function prepareSchemaData(items) {
//promise
}
function someNonPromiseFunc() {
//returns some Object
}
function updateListItems(items) {
//updates items using data from the object returned by someNonPromiseFunc
//promise
}
getListItems('aList')
.then(prepareSchemaData)
.then(someNonPromiseFunc)
.then(updateListItems);
显然,我不能像那样调用非承诺函数,但我需要确保在updateListItems 可以运行之前,someNonPromiseFunc 可以构建它的对象并完全返回。
【问题讨论】:
-
"显然,我不能像那样调用非承诺函数" - 是什么让你这么认为?
-
你也可以写
getListItems('aList').then(prepareSchemaData).then(data => updateListItems(someNonPromiseFunc(data)));
标签: javascript jquery asynchronous promise