Skip to main content
Code Review

Return to Answer

Commonmark migration
Source Link

#asyncawait and promise

asyncawait and promise

One way to do this is to use promises and an async function.

async functions stop execution each time they encounter an await token and "await" the promise (if any) to resolve before continuing. async functions are also promises themselves so you can await an async function as well.

#Example

Example

The example uses a timer event to resolve a promise. The function threeStep uses the wait function to pause execution for the set time.

To show how you can also chain the async function It repeats the 3 steps after the first lot are done by using the async function's returned promise to start the second three steps.

const wait = time => new Promise(tick => setTimeout(tick, time));
async function threeStep(time) {
 log(`Step A and wait ${time}ms`);
 await wait(time);
 log(`Step B and wait ${time}ms`);
 await wait(time);
 log(`Step C all done`); 
}
threeStep(2000)
 .then(() => {
 log("Do it again");
 threeStep(1000); 
 });
// a very hacky log function, don't try this at home. :P
function log(data) { logText.innerHTML += data + "<br>" }
<div id="logText"></div>

#asyncawait and promise

One way to do this is to use promises and an async function.

async functions stop execution each time they encounter an await token and "await" the promise (if any) to resolve before continuing. async functions are also promises themselves so you can await an async function as well.

#Example

The example uses a timer event to resolve a promise. The function threeStep uses the wait function to pause execution for the set time.

To show how you can also chain the async function It repeats the 3 steps after the first lot are done by using the async function's returned promise to start the second three steps.

const wait = time => new Promise(tick => setTimeout(tick, time));
async function threeStep(time) {
 log(`Step A and wait ${time}ms`);
 await wait(time);
 log(`Step B and wait ${time}ms`);
 await wait(time);
 log(`Step C all done`); 
}
threeStep(2000)
 .then(() => {
 log("Do it again");
 threeStep(1000); 
 });
// a very hacky log function, don't try this at home. :P
function log(data) { logText.innerHTML += data + "<br>" }
<div id="logText"></div>

asyncawait and promise

One way to do this is to use promises and an async function.

async functions stop execution each time they encounter an await token and "await" the promise (if any) to resolve before continuing. async functions are also promises themselves so you can await an async function as well.

Example

The example uses a timer event to resolve a promise. The function threeStep uses the wait function to pause execution for the set time.

To show how you can also chain the async function It repeats the 3 steps after the first lot are done by using the async function's returned promise to start the second three steps.

const wait = time => new Promise(tick => setTimeout(tick, time));
async function threeStep(time) {
 log(`Step A and wait ${time}ms`);
 await wait(time);
 log(`Step B and wait ${time}ms`);
 await wait(time);
 log(`Step C all done`); 
}
threeStep(2000)
 .then(() => {
 log("Do it again");
 threeStep(1000); 
 });
// a very hacky log function, don't try this at home. :P
function log(data) { logText.innerHTML += data + "<br>" }
<div id="logText"></div>

Post Undeleted by Blindman67
added 702 characters in body
Source Link
Blindman67
  • 22.8k
  • 2
  • 16
  • 40

#asyncawait and promise

One way to do this is to use promisesuse promises and an async functionasync function.

async functions stop execution each time they encounter aan await token and "await" the promise (if any) to resolve before continuing. async functions are also promises themselves so you can await an async function as well.

#Example

The example uses a timer event to resolve a promise. The function threeStep uses the wait function to pause execution for the set time.

To show how you can also chain the async function It repeats the 3 steps after the first lot are done by using the async function's returned promise to start the second three steps.

const wait = time => new Promise(tick => setTimeout(tick, time));
async function threeStep(time) {
 log(`Step A and wait ${time}ms`);
 await wait(time);
 log(`Step B and wait ${time}ms`);
 await wait(time);
 log(`Step C all done`); 
}
threeStep(2000)
 .then(() => {
 log("Do it again"); threeStep(1000); 
 });

// a very hacky log function, don't try this at home. :P
function log(data){ logText.innerHTML += data + "<br>" }
<div id="logText"></div>

One way to do this is to use promises and an async function.

async functions stop execution each time they encounter a await token and "await" the promise (if any) to resolve before continuing. async functions are also promises themselves so you can await an async function as well

const wait = time => new Promise(tick => setTimeout(tick, time));
async function threeStep(time) {
 log(`Step A and wait ${time}ms`);
 await wait(time);
 log(`Step B and wait ${time}ms`);
 await wait(time);
 log(`Step C all done`); 
}
threeStep(2000); 
// very hacky log function
function log(data){ logText.innerHTML += data + "<br>" }
<div id="logText"></div>

#asyncawait and promise

One way to do this is to use promises and an async function.

async functions stop execution each time they encounter an await token and "await" the promise (if any) to resolve before continuing. async functions are also promises themselves so you can await an async function as well.

#Example

The example uses a timer event to resolve a promise. The function threeStep uses the wait function to pause execution for the set time.

To show how you can also chain the async function It repeats the 3 steps after the first lot are done by using the async function's returned promise to start the second three steps.

const wait = time => new Promise(tick => setTimeout(tick, time));
async function threeStep(time) {
 log(`Step A and wait ${time}ms`);
 await wait(time);
 log(`Step B and wait ${time}ms`);
 await wait(time);
 log(`Step C all done`); 
}
threeStep(2000)
 .then(() => {
 log("Do it again"); threeStep(1000); 
 });

// a very hacky log function, don't try this at home. :P
function log(data){ logText.innerHTML += data + "<br>" }
<div id="logText"></div>

Post Deleted by Blindman67
Source Link
Blindman67
  • 22.8k
  • 2
  • 16
  • 40

One way to do this is to use promises and an async function.

async functions stop execution each time they encounter a await token and "await" the promise (if any) to resolve before continuing. async functions are also promises themselves so you can await an async function as well

const wait = time => new Promise(tick => setTimeout(tick, time));
async function threeStep(time) {
 log(`Step A and wait ${time}ms`);
 await wait(time);
 log(`Step B and wait ${time}ms`);
 await wait(time);
 log(`Step C all done`);
 
}
threeStep(2000); 
// very hacky log function
function log(data){ logText.innerHTML += data + "<br>" }
<div id="logText"></div>

default

AltStyle によって変換されたページ (->オリジナル) /