The following two functions exchange promise-resolution data. The second function is supposed to return the object to the first function's internal promise's then, but it doesn't.
let functionA = function (stuff) {
var responseToGet;
return fetch(someURL
{
method: 'GET',
headers: {headers},
})
.then((resp) => {
responseToGet = resp;
return functionB(responseToGet)
})
.then((respFromB) => {
console.log('respFromB', respFromB); // WHY IS THIS UNDEFINED INSTEAD OF TRUE/FALSE ?
if (respFromB.status)
return null;
else
return respFromB.jsonObj;
})
.catch((error) => {
console.log(error);
});}
let functionB = function(response)
{
response.json().then((r2) => {
if (something)
return Promise.resolve({status: true, jsonObj: null});
else
return Promise.resolve({status: false, jsonObj: r2});
})
.done();}
you don't need to call 'done' function. Just return a promise from functionB
Example:
var functionA = function (stuff) {
var responseToGet;
return fetch("http://httpbin.org/gzip",
{
method: 'GET',
headers: {}
})
.then(function (resp) {
responseToGet = resp;
return functionB(responseToGet)
}).then(function (respFromB) {
console.log('respFromB', respFromB); // WHY IS THIS UNDEFINED INSTEAD OF TRUE/FALSE ?
if (respFromB.status)
return null;
else
return respFromB.jsonObj;
}).catch(function (error) {
console.log(error);
});
}
var functionB = function (response) {
var promise = response.json().then(function (r2) {
if (r2)
return Promise.resolve({status: true, jsonObj: null});
else
return Promise.resolve({status: false, jsonObj: r2});
});
return promise;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.