Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* When you mention the popup I imagine it is already an async function.
- When the prompt is cicked, then it will call a callback, and if it is promisfied then that callback will a function that resolves a promise.
- setTimeOut(cbFunction, 5000) //setTimeOut takes a callback function and runs that function after x amount of ms
- If you wanted to use it with promises you can create a wrapper function that returns a promise, and the callback function is now becomes a function that resolves the promise by invoking the resolve function, you can get in the first parameter of a function you need to pass in when you make a new Promise. */
- function promisifiedTimeout(ms) {
- return new Promise((resolve) => {
- setTimeout(() => resolve(), ms);
- });
- }
- // It sounds confusing but once a function is promisified, it's not that difficult to chain the execution however you want. Let's say I had 3 async tasks that took time A, B. And for each one it was a function that returned a promise.
- function A(){
- return new Promise((resolve) =>{
- setTimeout(() => resolve(), 3000);
- })
- } //...etc
- // Well in my workflow I'd make it an asynchrnous function
- async function workflow() {
- //if I wanted something to happen before anything else
- await A()
- //now any code below this line won't run till A has resolved
- //if I then wanted B and C to both start to resolve but to wait till they're both finished
- await promise.all([B(), C()]) //promise.all lets us give an array of promises and will only resolve once they're all resolved
- //if you wanted to run code inbetween parts of this chain you'd just write it inbetween the awaits where it belongs in the timeline
- }
- /* Instead of your popup function starting workflow B once it's clicked, you could get it to resolve a promise that after being resolved will start workflow B
- workflow B will also resolve a promise when it is done. So the rest of Workflow A knows it can be executed, and the rest of A won't executre early because it will be awaiting the resolution of Workflow B.
- Here's some messy code as an example */
- async function workflowA() {
- await Promise.all([delay(2000, "A before b resolves"), workflowBPopup()]);
- await delay(2000, "A after b resolves");
- return "A resolved";
- }
- async function workflowBPopup() {
- await delay(5000, "awaiting popup click");
- await workflowB();
- return "B resolved";
- }
- async function workflowB() {
- delay(2000, "B");
- }
- function delay(ms, workflowName) {
- console.log(`doing ${workflowName}... ${Date.now() - start}ms`);
- return new Promise((resolve) => {
- setTimeout(() => {
- console.log(`${workflowName} has been completed ${Date.now() - start}ms`);
- resolve();
- }, ms);
- });
- }
- const start = Date.now();
- const workflow = await workflowA();
- console.log("All Resolved");
- /* --console-log
- doing A before b resolves... 1ms
- doing awaiting popup click... 35ms
- A before b resolves has been completed 2037ms
- awaiting popup click has been completed 5039ms
- doing B... 5040ms
- doing A after b resolves... 5041ms
- B has been completed 7043ms
- A after b resolves has been completed 7044ms
- All Resolved */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement