3 ms·
Inside an async block, the await keyword stands for then/flatMap/bind. Using multiple awaits is the same as a chain of then/flatMap/bind, or the do-notation in
by manx 5y ago
Inside an async block, the await keyword stands for then/flatMap/bind. Using multiple awaits is the same as a chain of then/flatMap/bind, or the do-notation in Haskell.
async { const a = await getA(); const b = await getB(a); return await getC(b); }
Is the same as:
getA().then((a) => getB(a)).then((b) => getC(b));
Observable is interesting, because it has multiple flatMap variations, like concatMap, switchMap etc.
There could be different await keywords that represent these operators. Example:
asyncObservable { const request = await myRequestStream(); const response = switchMap sendRequest(request); return response; }
This should be equivalent to:
myRequestStream().switchMap((request) => sendRequest(request))
In general, the advantage of this syntax is, that you can work on die values directly, instead of needing to work with the Observable.
- manx 5y agoReact hooks with useState are very similar.