Skip to content

Instantly share code, notes, and snippets.

@xgqfrms-GitHub
Last active June 8, 2017 17:23
Show Gist options
  • Save xgqfrms-GitHub/0b568d347fdf84fb07119491610efc92 to your computer and use it in GitHub Desktop.
Save xgqfrms-GitHub/0b568d347fdf84fb07119491610efc92 to your computer and use it in GitHub Desktop.
async function & async callback js & ES7 & promise

async function & async callback js & ES7

https://caolan.github.io/async/docs.html

https://caolan.github.io/async/

https://github.com/caolan/async

async function & promise

https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Statements/async_function

async function 声明定义了一个异步函数,它返回一个 AsyncFunction 对象。

你还可以定义异步函数, 使用一个 async function expression

function resolveAfter2Seconds(x) {
  return new Promise(resolve => {
    setTimeout(() => {
      resolve(x);
    }, 2000);
  });
}

async function add1(x) {
  var a = resolveAfter2Seconds(20);
  var b = resolveAfter2Seconds(30);
  return x + await a + await b;
}

add1(10).then(v => {
  console.log(v);  // prints 60 after 2 seconds.
});

async function add2(x) {
  var a = await resolveAfter2Seconds(20);
  var b = await resolveAfter2Seconds(30);
  return x + a + b;
}

add2(10).then(v => {
  console.log(v);  // prints 60 after 4 seconds.
});

通过异步方法重写 promise 链

function getProcessedData(url) {
  return downloadData(url) // returns a promise
    .catch(e => {
      return downloadFallbackData(url) // returns a promise
    })
    .then(v => {
      return processDataInWorker(v); // returns a promise
    });
}


// equal to 


async function getProcessedData(url) {
  let v:
  try {
    v = await downloadData(url); 
  } catch (e) {
    v = await downloadFallbackData(url);
  }
  return processDataInWorker(v);
}



https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Operators/await

promise

https://www.promisejs.org/

https://promisesaplus.com/

https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Promise

https://developers.google.com/web/fundamentals/getting-started/primers/promises?hl=zh-cn

https://api.jquery.com/promise/

http://exploringjs.com/es6/ch_promises.html

https://medium.com/javascript-scene/master-the-javascript-interview-what-is-a-promise-27fc71e77261

Asynchronous programming in Node.js

https://blog.risingstack.com/node-js-async-best-practices-avoiding-callback-hell-node-js-at-scale/

Callback Hell

http://callbackhell.com/

fs.readdir(source, function (err, files) {
  if (err) {
    console.log('Error finding files: ' + err)
  } else {
    files.forEach(function (filename, fileIndex) {
      console.log(filename)
      gm(source + filename).size(function (err, values) {
        if (err) {
          console.log('Error identifying file size: ' + err)
        } else {
          console.log(filename + ' : ' + values)
          aspect = (values.width / values.height)
          widths.forEach(function (width, widthIndex) {
            height = Math.round(width / aspect)
            console.log('resizing ' + filename + 'to ' + height + 'x' + height)
            this.resize(width, height).write(dest + 'w' + width + '_' + filename, function(err) {
              if (err) console.log('Error writing file: ' + err)
            })
          }.bind(this))
        }
      })
    })
  }
})
@xgqfrms-GitHub
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment