如何在 javascript 中使用承诺? Javascript 中的基本承诺
How to use promises in javascript ? Basic Promise in Javascript
我正在使用 redux。我想在更新商店后执行某个网络调用。
我为此写了承诺 -
let promise=new Promise(function(resolve,reject){
updateStore() //separate method which updates the store.
}).then(()=>{
//Netowrk call
//axios code
});
我在这里遇到语法错误:需要声明或语句。
执行此操作的方法应该是什么activity?
你可以但不确定为什么要在这里使用 promise?我们还在等什么?
function updateStore() {
console.log("updating");
}
let promise=new Promise(function(resolve,reject){
console.log('update')
resolve( updateStore() ) //separate method which updates the store.
}).then(()=>{
console.log('now network')
//Netowrk call
//axios code
});
Using promise
使用函数可能会更好。
function update_store(callback) {
console.log("update store");
callback(); /* or setTimeout( callback ); */
}
function now_network() {
console.log("update network");
}
update_store(now_network);
Using callback
我正在使用 redux。我想在更新商店后执行某个网络调用。 我为此写了承诺 -
let promise=new Promise(function(resolve,reject){
updateStore() //separate method which updates the store.
}).then(()=>{
//Netowrk call
//axios code
});
我在这里遇到语法错误:需要声明或语句。
执行此操作的方法应该是什么activity?
你可以但不确定为什么要在这里使用 promise?我们还在等什么?
function updateStore() {
console.log("updating");
}
let promise=new Promise(function(resolve,reject){
console.log('update')
resolve( updateStore() ) //separate method which updates the store.
}).then(()=>{
console.log('now network')
//Netowrk call
//axios code
});
Using promise
使用函数可能会更好。
function update_store(callback) {
console.log("update store");
callback(); /* or setTimeout( callback ); */
}
function now_network() {
console.log("update network");
}
update_store(now_network);
Using callback