Js: Promises

Async/Await, Promise.resolve/reject, Generators. Good article on generators: https://hackernoon.com/redux-saga-tutorial-for-beginners-and-dog-lovers-aa69a17db645

12 flashcards · Programming Languages · by lakesare

  1. Promise.resolve(1) .then((x) => x + 1) .then((x) => { throw new Error('Hi') }) .catch(() => 1) .then((x) => x + 1) .then((x) => console.log(x)) .catch(console.error) What will be returned?

    A new Promise is created, that will resolve to 1. The resolved value is incremented by 1 (so it is 2 now), and returned instantly. The resolved value is discarded, and an error is thrown. The error is discarded, and a new value (1) is returned. The execution did not stop after the catch, but before the exception was handled, it continued, and a new, incremented value (2) is returned. The value is printed to the standard output. This line won't run, as there was no exception.

  2. Resolves or rejects as soon as one of the promises in the iterable resolves or rejects?

    Promise.race(iterable)

  3. What's the result? const hi = async () => { return 2; }; console.log(hi()); // => ...?

    Promise { 2 }

  4. What's the result? const hi = async () => { throw new Error('rrr'); }; console.log(hi());

    result returned: => Promise {<rejected>: Error: rrr at ...} and an error thrown: Uncaught (in promise) Error: rrr (Same as from Promise.reject('rrr'))

  5. How to get 10 and 20 now? function* rrr(i) { yield i; yield i + 10; } var generator_object = rrr(10);

    generator_object.next().value; //=> 10 generator_object.next().value; //=> 20

  6. How is rrr called? function* rrr(i) { yield i; yield i + 10; }

    A generator function.

  7. What are generators in Js?

    Generators are functions which can be exited and later re-entered. Their context (variable bindings) will be saved across re-entrances.

  8. function* rrr() { yield 'hi'; yield 'hello'; } generator_object = rrr(); generator_object.next(); //=> ??? generator_object.next(); //=> ???

    generator_object.next(); //=> { value: "hi", done: false } generator_object.next(); //=> { value: "hello", done: false } generator_object.next(); //=> { value: undefined, done: true }

  9. What generator_object.next().value contains?

    Whatever goes after yield, e.g. yield 'Hiii'.

  10. What does watcher saga do? export function* watcherSaga() { yield takeLatest("API_CALL_REQUEST", workerSaga); }

    Watches for actions dispatched to the store, starts worker saga.

  11. What does takeLatest do? import { takeLatest } from "redux-saga/effects"; export function* watcherSaga() { yield takeLatest("API_CALL_REQUEST", workerSaga); }

    takeLatest is a helper function provided by redux-saga that will trigger a new workerSaga when it sees an API_CALL_REQUEST, while cancelling any previously triggered workerSaga still in process.

  12. What does worker saga do? function* workerSaga() { try { const response = yield call(fetchDog); const dog = response.data.message; // dispatch a success action to the store yield put({ type: "API_CALL_SUCCESS", dog }); } catch (error) { // dispatch a failure action to the store yield put({ type: "API_CALL_FAILURE", error }); } }

    Worker saga makes the api call when watcher saga sees the action.