How to use the __await function from tslib

Find comprehensive JavaScript tslib.__await code examples handpicked from public code repositorys.

tslib.__await is a keyword used in asynchronous generator functions to pause the execution of a generator until a promise is resolved.

882
883
884
885
886
887
888
889
890
891
        }
    }
    return [4 /*yield*/, tslib.__await(restClient.invoke('GET', "/email-validations", params, undefined, undefined, cancellationToken))];
case 2:
    response = _d.sent();
    return [4 /*yield*/, tslib.__await(response.deserialize())];
case 3:
    // TODO: Check the response status code
    listSegment = _d.sent();
    _b = 0, _c = listSegment.data;
fork icon2
star icon8
watch icon2

+ 23 other calls in file

How does tslib.__await work?

In an asynchronous generator function, the tslib.__await keyword is used to pause the execution of the generator function until a promise is resolved. Here's how it works in detail: When the generator function encounters the tslib.__await keyword, it suspends its execution and returns an object with a next method. When the promise passed to tslib.__await is resolved, the next method of the suspended generator is called with the resolved value as an argument. The generator function resumes its execution, and the value passed to next is returned from the yield statement that contained the tslib.__await keyword. If the promise passed to tslib.__await is rejected, the throw method of the suspended generator is called with the rejection reason as an argument, and the generator function throws an error. Here's an example of using tslib.__await in an asynchronous generator function: javascript Copy code {{{{{{{ async function exampleAsyncGenerator() { const value = await Promise.resolve('hello'); const otherValue = await Promise.resolve('world'); const result = await tslib.__await(Promise.resolve(value + ' ' + otherValue)); return result; } const generator = exampleAsyncGenerator(); generator.next().then((result) => { console.log(result); // { value: 'hello', done: false } return generator.next(); }).then((result) => { console.log(result); // { value: 'world', done: false } return generator.next(); }).then((result) => { console.log(result); // { value: 'hello world', done: true } }); In this example, the exampleAsyncGenerator function is an asynchronous generator function that uses the tslib.__await keyword to pause the execution of the generator function until a promise is resolved. The generator function returns the concatenated value of two resolved promises. The generator function is called and its next method is used to retrieve the value of each yield statement. When the generator function is finished, the final value is logged to the console.

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
13
async function* myAsyncGenerator() {
  const result1 = await Promise.resolve(1);
  console.log(result1);
  const result2 = await tslib.__await(Promise.resolve(2));
  console.log(result2);
  yield result1 + result2;
}

(async () => {
  for await (const value of myAsyncGenerator()) {
    console.log(value);
  }
})();

In this example, the myAsyncGenerator function is an asynchronous generator function that uses both await and tslib.__await to pause the execution of the generator function until a promise is resolved. The generator function returns the sum of the values resolved by two promises. The first promise is resolved using the await keyword, while the second promise is resolved using tslib.__await. The for await...of loop is used to iterate over the values generated by the myAsyncGenerator function. When the loop is executed, the result1 value of 1 is logged to the console, then the tslib.__await statement pauses the generator function until the promise resolves. Once the promise resolves, the result2 value of 2 is logged to the console, and the sum of result1 and result2 (3) is yielded to the for await...of loop. Finally, the value of 3 is logged to the console.