demos/node-cli-item-management/index.js
async function deleteItems(items, session) {
  // create our iterator which should return a `Promise`
  const asyncIterable = {
    [Symbol.asyncIterator]: () => ({
      // every time we ask for a new item
      next: function() {
        // remove the next item from the array
        const item = items.pop();

        // we are done if there is no item
        if (!item) {
          return Promise.resolve({ done: true });
        }

        // prompt to delete this item.
        return deleteItem(item, session);
      }
    })
  };

  // lets get our `next` function so we can itterate over it
  const { next } = asyncIterable[Symbol.asyncIterator]();

  for (
    let { done, deleted, item } = await next(); // start condition, call next and wait for the `Promise` to resolve, results are destructed.
    !done; // end condition,
    { done, deleted, item } = await next() // action on every loop, sets up the next round of checks
  ) {
    if (deleted) {
      console.log(chalk.green(`${item.title} deleted successfuly\n`));
    } else {
      console.log(chalk.gray(`${item.title} skipped\n`));
    }
  }

  // since this is inside an async function code execution is stopped at the loop
  // this will return an empty resolved `Promise` when the loop is done.
  return Promise.resolve();
}
async function deleteItems(items, session) {
  // create our iterator which should return a `Promise`
  const asyncIterable = {
    [Symbol.asyncIterator]: () => ({
      // every time we ask for a new item
      next: function() {
        // remove the next item from the array
        const item = items.pop();

        // we are done if there is no item
        if (!item) {
          return Promise.resolve({ done: true });
        }

        // prompt to delete this item.
        return deleteItem(item, session);
      }
    })
  };

  // lets get our `next` function so we can itterate over it
  const { next } = asyncIterable[Symbol.asyncIterator]();

  for (
    let { done, deleted, item } = await next(); // start condition, call next and wait for the `Promise` to resolve, results are destructed.
    !done; // end condition,
    { done, deleted, item } = await next() // action on every loop, sets up the next round of checks
  ) {
    if (deleted) {
      console.log(chalk.green(`${item.title} deleted successfuly\n`));
    } else {
      console.log(chalk.gray(`${item.title} skipped\n`));
    }
  }

  // since this is inside an async function code execution is stopped at the loop
  // this will return an empty resolved `Promise` when the loop is done.
  return Promise.resolve();
}
async function deleteItems ( items , session ) { // create our iterator which should return a `Promise` const asyncIterable = { [ Symbol . asyncIterator ] : ( ) => ( { // every time we ask for a new item next : function ( ) { // remove the next item from the array const item = items . pop ( ) ; // we are done if there is no item if ( ! item ) { return Promise . resolve ( { done : true } ) ; } // prompt to delete this item. return deleteItem ( item , session ) ; } } ) } ; // lets get our `next` function so we can itterate over it const { next } = asyncIterable [ Symbol . asyncIterator ] ( ) ; for ( let { done , deleted , item } = await next ( ) ; // start condition, call next and wait for the `Promise` to resolve, results are destructed. ! done ; // end condition, { done , deleted , item } = await next ( ) // action on every loop, sets up the next round of checks ) { if ( deleted ) { console . log ( chalk . green ( ` ${ item . title } \n ` ) ) ; } else { console . log ( chalk . gray ( ` ${ item . title } \n ` ) ) ; } } // since this is inside an async function code execution is stopped at the loop // this will return an empty resolved `Promise` when the loop is done. return Promise . resolve ( ) ; }
this give a list of `items` and a `UserSession` this will ask us 1 at a time if we want to delte this item. This uses the new async iteration syntax with `Symbol.asyncIterator` to perform an async function in a loop
this give a list of items and a UserSession this will ask us 1 at a time if we want to delte this item . This uses the new async iteration syntax with Symbol . asyncIterator to perform an async function in a loop