我正在使用新的office.js.我正在使用返回promise的Excel.run功能.我对库实现的promises模式有疑问.
样本都显示了这种模式
Excel.run( function (ctx) {
//set up something
return ctx.sync().then (function () {
//call another function somewhere to chain operations
});
}).then ( function () {
//do something else if you want
}).catch (function (error) {
handle errors
});
Run Code Online (Sandbox Code Playgroud)
问题是Excel.run()中包含的ctx.sync().then()它呈现的方式,你不能按照promises规范链接promises,因为如果你试图处理它就丢失了上下文对象然后()在Excel.run()之外()因此,模式似乎是在促进嵌套函数调用,这是承诺应该消除的.
我想要做的是通过链接将几个调用一起排序:
Excel.run( function (ctx) {
return ctx.sync();
}).then ( function (ctx) {
return ctx.sync();
}).then ( function (ctx) {
return ctx.sync();
}).then ( function (ctx) {
return ctx.sync();
}).catch (function (error) {
});
Run Code Online (Sandbox Code Playgroud)
这可能吗?