promise

assign resolve values to global variables - promises [duplicate]

白昼怎懂夜的黑 提交于 2020-08-26 13:49:08
问题 This question already has answers here : How do I return the response from an asynchronous call? (39 answers) Closed 3 years ago . Rather than use callbacks, I decided to use promises. I resolve the variable which holds a price and then I call .then to handle it like this. SomeAsyncThing("fifa 17").then(function(value){ console.log("value " + value); }); How can I assign the value to a global variable to use elsewhere in JS? Am I missing something obvious? I've tried defining x = 0; at the

What is correct way to handle fetch response

南楼画角 提交于 2020-08-23 08:05:19
问题 I have following code which I using for handling Magento 2 REST API. return new Promise((resolve, reject) => { fetch(uri, { method, headers, body: JSON.stringify(data) }) .then(response => { return response.json(); }) .then(responseData => { resolve(responseData); }) .catch(error => { reject(error); }); }); And I want to add response status checking, so I've started like this return new Promise((resolve, reject) => { fetch(uri, { method, headers, body: JSON.stringify(data) }) .then(response =