I am watching the Koa.js / Node.js application and I think I am well versed in generators and promises. But I canβt wrap my head in the following code:
function *parseAuthorization() { let parameters = this.query; let accessToken = yield storakleShopifyApi.exchangeTemporaryToken(parameters); if(accessToken) { return ... } return this.response.redirect("/home/"); };
The exchangeTemporaryToken method is as follows:
function* exchangeTemporaryToken(query) { let authApi = getAuthApi(query.shop); return new Promise(function (resolve, reject) { authApi.exchange_temporary_token(query, function (err, data) { if (err) { return reject(err); } return resolve(data['access_token']); }); }); };
* parseAuthorization is obviously a generator function (an API action in this case) that blocks on this line:
let accessToken = yield storakleShopifyApi.exchangeTemporaryToken(parameters);
the storakleShopifyApi.exchangeTemporaryToken is another generator function that returns Promise quite interestingly.
But the solution itself does not understand promises, does it? I also assume that the call:
storakleShopifyApi.exchangeTemporaryToken(parameters);
Return:
IteratorResult {value: Promise..., done: true}
So how to do this by processing this and assigning the allowed value from the promise of the variable accessToken?
source share