Advertisement
Guest User

Untitled

a guest
Mar 26th, 2025
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* When you mention the popup I imagine it is already an async function.
  2. 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.
  3. setTimeOut(cbFunction, 5000) //setTimeOut takes a callback function and runs that function after x amount of ms
  4. 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.  */
  5. function promisifiedTimeout(ms) {
  6.   return new Promise((resolve) => {
  7.     setTimeout(() => resolve(), ms);
  8.   });
  9. }
  10.  
  11. // 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.
  12. function A(){
  13.     return new Promise((resolve) =>{
  14.         setTimeout(() => resolve(), 3000);
  15.     })
  16. } //...etc
  17. // Well in my workflow I'd make it an asynchrnous function
  18. async function workflow() {
  19.   //if I wanted something to happen before anything else
  20.   await A()
  21.   //now any code below this line won't run till A has resolved
  22.  
  23.   //if I then wanted B and C to both start to resolve but to wait till they're both finished
  24.  
  25.   await promise.all([B(), C()]) //promise.all lets us give an array of promises and will only resolve once they're all resolved
  26.  
  27.   //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
  28. }
  29. /* 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
  30. 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.
  31. Here's some messy code as an example */
  32. async function workflowA() {
  33.   await Promise.all([delay(2000, "A before b resolves"), workflowBPopup()]);
  34.   await delay(2000, "A after b resolves");
  35.   return "A resolved";
  36. }
  37.  
  38. async function workflowBPopup() {
  39.   await delay(5000, "awaiting popup click");
  40.   await workflowB();
  41.   return "B resolved";
  42. }
  43.  
  44. async function workflowB() {
  45.   delay(2000, "B");
  46. }
  47.  
  48. function delay(ms, workflowName) {
  49.   console.log(`doing ${workflowName}... ${Date.now() - start}ms`);
  50.   return new Promise((resolve) => {
  51.     setTimeout(() => {
  52.       console.log(`${workflowName} has been completed ${Date.now() - start}ms`);
  53.       resolve();
  54.     }, ms);
  55.   });
  56. }
  57.  
  58.  
  59.  
  60. const start = Date.now();
  61. const workflow = await workflowA();
  62. console.log("All Resolved");
  63.  
  64.  
  65. /* --console-log
  66. doing A before b resolves... 1ms
  67. doing awaiting popup click... 35ms
  68. A before b resolves has been completed 2037ms
  69. awaiting popup click has been completed 5039ms
  70. doing B... 5040ms
  71. doing A after b resolves... 5041ms
  72. B has been completed 7043ms
  73. A after b resolves has been completed 7044ms
  74. All Resolved */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement