如何 return 数据和承诺
How to return data along with a promise
此函数像状态机一样按顺序处理股票账户数据,以便下卖单。
我需要将帐户数据传递给每个州,我不想将其存储在全局变量中。我该如何实现?我是否不恰当地使用了承诺?
注意像get_account、delete_order这样的每个调用都是returns承诺的异步调用.他们不应该传递不相关的数据。
function sell_stocks(){
get_account_info().then(account => {
if (account.orders.length > 0){
return delete_orders(account.orders);
} else {
return "continue";
}
}).then(data => {
// Now I need variable "account"
place_sell_order(account.volume); //account undefined
}).catch(msg => {
//handle error
});
}
干什么
return delete_orders(account.orders).then((data) => [data,account]);
和
then(([data, account]) => {...})
或者如果您只想要帐户数据
return delete_orders(account.orders).then(() => account);
和
function sell_stocks(){
get_account_info().then(account => {
if (account.orders.length > 0){
return delete_orders(account.orders).then(()=> account);
} else {
return account;
}
}).then(account => {
place_sell_order(account.volume);
}).catch(msg => {
//handle error
});
}
或 async/await
async function sell_stocks(){
try {
const account = await get_account_info();
if (account.orders.length > 0) {
await delete_orders(account.orders)
}
return place_sell_order(account.volume);
} catch (e) {
console.log(`At sell_stocks ${e}`);
return null;
}
}
Return account
从第一个 .then()
到链接 .then()
访问 account
function sell_stocks(){
return get_account_info().then(account => {
if (account.orders.length > 0){
return account
} else {
return "continue";
}
}).then(data => {
// Now I need variable "account"
return delete_orders(data.orders)
.then(() => place_sell_order(data.volume))
}).catch(msg => {
//handle error
});
}
您可以进行如下操作:
function sell_stocks() {
get_account_info().then((account) => {
return new Promise((resolve) => {
if (account.orders.length > 0) {
resolve(delete_orders(account.orders));
} else {
resolve('continue');
}
}).then(data => {
// Now I need variable "account"
place_sell_order(account.volume); //account undefined
}).catch(msg => {
//handle error
});
});
}
因此将 account
变量保留在本地范围内。
此函数像状态机一样按顺序处理股票账户数据,以便下卖单。
我需要将帐户数据传递给每个州,我不想将其存储在全局变量中。我该如何实现?我是否不恰当地使用了承诺?
注意像get_account、delete_order这样的每个调用都是returns承诺的异步调用.他们不应该传递不相关的数据。
function sell_stocks(){
get_account_info().then(account => {
if (account.orders.length > 0){
return delete_orders(account.orders);
} else {
return "continue";
}
}).then(data => {
// Now I need variable "account"
place_sell_order(account.volume); //account undefined
}).catch(msg => {
//handle error
});
}
干什么
return delete_orders(account.orders).then((data) => [data,account]);
和
then(([data, account]) => {...})
或者如果您只想要帐户数据
return delete_orders(account.orders).then(() => account);
和
function sell_stocks(){
get_account_info().then(account => {
if (account.orders.length > 0){
return delete_orders(account.orders).then(()=> account);
} else {
return account;
}
}).then(account => {
place_sell_order(account.volume);
}).catch(msg => {
//handle error
});
}
或 async/await
async function sell_stocks(){
try {
const account = await get_account_info();
if (account.orders.length > 0) {
await delete_orders(account.orders)
}
return place_sell_order(account.volume);
} catch (e) {
console.log(`At sell_stocks ${e}`);
return null;
}
}
Return account
从第一个 .then()
到链接 .then()
account
function sell_stocks(){
return get_account_info().then(account => {
if (account.orders.length > 0){
return account
} else {
return "continue";
}
}).then(data => {
// Now I need variable "account"
return delete_orders(data.orders)
.then(() => place_sell_order(data.volume))
}).catch(msg => {
//handle error
});
}
您可以进行如下操作:
function sell_stocks() {
get_account_info().then((account) => {
return new Promise((resolve) => {
if (account.orders.length > 0) {
resolve(delete_orders(account.orders));
} else {
resolve('continue');
}
}).then(data => {
// Now I need variable "account"
place_sell_order(account.volume); //account undefined
}).catch(msg => {
//handle error
});
});
}
因此将 account
变量保留在本地范围内。