@heyrohit/

Tasks Scheduler

Nodejs

No description

fork
loading
Files
  • index.js

This Plugin Crashed!

Error: Error: must not create an existing file {"type":"CREATE_FILE","wid":"0.3899883273752769","path":"index.js","file":{"path":"index.js","content":{"asEncoding":{"base64":"Y29uc3QgdGFza3MgPSBbCiAgewogICAgbmFtZTogJ3Rhc2stMScsCiAgICB0aW1lOiAyMDAKICB9LAogIHsKICAgIG5hbWU6ICd0YXNrLTInLAogICAgdGltZTogNDAwCiAgfSwKICB7CiAgICBuYW1lOiAndGFzay0zJywKICAgIHRpbWU6IDEwMAogIH0sCiAgewogICAgbmFtZTogJ3Rhc2stNCcsCiAgICB0aW1lOiA1MAogIH0KXTsKCgoKZnVuY3Rpb24gZXhlY3V0ZVRhc2tzKHRhc2tzKSB7CiAgcmV0dXJuIHRhc2tzLnJlZHVjZSgoYWNjdW11bGF0b3IsIHRhc2spID0+IHsKICAgIHJldHVybiBhY2N1bXVsYXRvci50aGVuKCgpID0+IGdldFByb21pc2UodGFzaykpOwogIH0sIFByb21pc2UucmVzb2x2ZSgpKTsKfQoKLy8gZXhlY3V0ZVRhc2tzKHRhc2tzKTsKCmZ1bmN0aW9uIGdldFByb21pc2UodGFzaykgewogIGNvbnN0IHRhc2tQcm9taXNlID0gbmV3IFByb21pc2UoKHJlc29sdmUsIHJlamVjdCkgPT4gewogICAgY29uc29sZS5sb2codGFzay5uYW1lICsgIiBzdGFydGVkLiIpOwogICAgCiAgICBzZXRUaW1lb3V0KCgpID0+IHsKICAgICAgY29uc29sZS5sb2codGFzay5uYW1lICsgIiBkb25lIGluICIsIHRhc2sudGltZSwgIm1zLiIpOwogICAgICByZXNvbHZlKHRhc2submFtZSk7CiAgICB9LCB0YXNrLnRpbWUpOwogIH0pOwogIAogIHJldHVybiB0YXNrUHJvbWlzZTsKfQoKLy8gVHlwZSAxCmZ1bmN0aW9uIHByb21pc2VSZXR1cm5zUHJvbWlzZSgpIHsKICByZXR1cm4gUHJvbWlzZS5yZXNvbHZlKCkudGhlbihmdW5jdGlvbiAoKXsKICAgIHJldHVybiBnZXRQcm9taXNlKHRhc2tzWzBdKTsKICB9KS50aGVuKGZ1bmN0aW9uICgpIHsKICAgIHJldHVybiBnZXRQcm9taXNlKHRhc2tzWzFdKTsKICAgIH0pOwp9Cgpwcm9taXNlUmV0dXJuc1Byb21pc2UoKTsKCi8vIFR5cGUgMgpmdW5jdGlvbiBwcm9taXNlRG9lc25vdFJldHVybigpIHsKICBQcm9taXNlLnJlc29sdmUoKS50aGVuKGZ1bmN0aW9uICgpewogICAgZ2V0UHJvbWlzZSh0YXNrc1swXSk7CiAgfSkudGhlbihmdW5jdGlvbiAoKSB7CiAgICBnZXRQcm9taXNlKHRhc2tzWzFdKTsKICAgIH0pOwp9CgovLyBwcm9taXNlRG9lc25vdFJldHVybigp"},"asBuffer":null},"loaded":true}}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
const tasks = [
  {
    name: 'task-1',
    time: 200
  },
  {
    name: 'task-2',
    time: 400
  },
  {
    name: 'task-3',
    time: 100
  },
  {
    name: 'task-4',
    time: 50
  }
];



function executeTasks(tasks) {
  return tasks.reduce((accumulator, task) => {
    return accumulator.then(() => getPromise(task));
  }, Promise.resolve());
}

// executeTasks(tasks);

function getPromise(task) {
  const taskPromise = new Promise((resolve, reject) => {
    console.log(task.name + " started.");
    
    setTimeout(() => {
      console.log(task.name + " done in ", task.time, "ms.");
      resolve(task.name);
    }, task.time);
  });
  
  return taskPromise;
}

// Type 1
function promiseReturnsPromise() {
  return Promise.resolve().then(function (){
    return getPromise(tasks[0]);
  }).then(function () {
    return getPromise(tasks[1]);
    });
}

promiseReturnsPromise();

// Type 2
function promiseDoesnotReturn() {
  Promise.resolve().then(function (){
    getPromise(tasks[0]);
  }).then(function () {
    getPromise(tasks[1]);
    });
}

// promiseDoesnotReturn()
node v10.16.0