reactjs – Unable to add multiple tasks using google tasks api in react.js

One thing to keep in mind is that the Google Tasks API only allows you to insert up to 100 tasks at a time.
1.Use a for loop

function addManualTasks() {
  const tasks = [
    { "title": "Task 1", "notes": "This is the first task" },
    { "title": "Task 2", "notes": "This is the second task" }
  ];

  for (let i = 0; i < tasks.length; i++) {
    gapi.client.tasks.tasks.insert({ tasklist: 'MTc0MTY5NjA3MTc2ODY1MDAwNjk6MDow', resource: tasks[i] }).then(res => console.log('res', res));
  }
}

2.Use Promise.all

function addManualTasks() {
  const tasks = [
    { "title": "Task 1", "notes": "This is the first task" },
    { "title": "Task 2", "notes": "This is the second task" }
  ];

  const promises = tasks.map(task => {
    return gapi.client.tasks.tasks.insert({ tasklist: 'MTc0MTY5NjA3MTc2ODY1MDAwNjk6MDow', resource: task });
  });

  Promise.all(promises).then(responses => console.log('responses', responses));
}

Read more here: Source link