I have code like this:
function fetchUsers(cb) {
fetch(API_BASE + '/users/').then(checkStatus)
.then(parseJSON)
.then(cb);
}
If I pass cb
so that it takes the JSON and stores it or applies it all is well.
What I would prefer is something like this:
function fetchUsers(cb) {
return fetch(API_BASE + '/users/').then(checkStatus)
.then(parseJSON)
.then(data => { return data });
}
But this does not work because the returned data
is still a Promise.
There are several questions on SO about Promises and fetch. NodeJS fetch promise callback pending or Get the data from fetch -> promise -> response for instance. But I could not find one that explains why the work has to happen within the then
call. Is there an idiomatic way to block until the Promise is resolved?