【发布时间】:2018-10-15 08:58:49
【问题描述】:
我正在使用Eddy's nativescript-secure-storage plugin 在设备存储(NativeScript 应用程序)中存储多个变量。
每个存储值的调用都会返回一个承诺:
this.secureStorage = new SecureStorage();
secureStorage.set({
key: "foo",
value: "val1"
}).then(
function(success) {
//move on to the next one.
});
存储多个值后,我需要将应用导航到主视图,但需要先确认值已成功存储。
我怎样才能避免嵌套这些调用(这确实有效,但对于很多值来说不会很漂亮):
this.secureStorage = new SecureStorage();
secureStorage.set({
key: "foo1",
value: "val1"
}).then(
function(success) {
secureStorage.set({
key: "foo2",
value: "val2"
}).then(
function(success) {
secureStorage.set({
key: "foo3",
value: "val3"
}).then(
function(success) {
//navigate to home view
});
});
});
【问题讨论】:
-
Promise.all(...)? -
@jonrsharpe 啊....like this
-
@lilbiscuit 正如 jonrsharpe 所说,构建一个 Promise 数组,然后调用
Promise.all(myarray),这使得 Promise 可以并行运行。 -
如果您需要知道任何承诺的结果,您可以在此处看到的 NS 应用程序中启用 async/await nativescript.org/blog/…
标签: angular nativescript angular-promise