我目前正在努力从一个链式事件返回一个值,这会导致Promise在挂起状态下返回。
我的代码返回一个特定Shopify产品的元域对象数组,然后我对其进行解析,并希望返回一个整数值。
但是,当我调试我的代码时,我得到的只是处于挂起状态的promise,而不是在作用域内的executed value。
我猜这在很大程度上是因为我对承诺不熟悉。
非常感谢您的帮助!
var quantity = shopify.metafield.list({
metafield: { owner_resource: 'product', owner_id: line_item.product_id }
}).then(metafields => new Promise(function(resolve, reject) {
//simplified code
resolve(2);
}))
.catch(error => console.log(error));
发布于 2018-06-07 02:32:19
shopify.metafield.list({
metafield: { owner_resource: 'product', owner_id: line_item.product_id }
}).then(metafields => new Promise(function(resolve, reject) {
//simplified code
resolve(2);
})).then((quantity)=> console.log(quantity))
.catch(error => console.log(error));
第二个.then()函数返回一个新的promise,因此您需要再次链接.then()函数,以便获得promise(resolve)返回的确切值
另一种方法是在包含第二个.then()函数返回的promise的数量变量上使用.then()函数
quantity.then((res)=> console.log(res))
https://stackoverflow.com/questions/50726371
复制相似问题