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

@xgqfrms-GitHub
Copy link
Author

xgqfrms-GitHub commented May 16, 2017

async function expression

https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Operators/async%E5%85%81%E8%AE%B8%E5%A3%B0%E6%98%8E%E4%B8%80%E4%B8%AA%E5%87%BD%E6%95%B0%E4%B8%BA%E4%B8%80%E4%B8%AA%E5%8C%85%E5%90%AB%E5%BC%82%E6%AD%A5%E6%93%8D%E4%BD%9C%E7%9A%84%E5%87%BD%E6%95%B0

AsyncFunction

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

AsyncFunction 构造函数 创建一个新的 async function 对象。
在JavaScript中,每个异步函数实际上都是一个 AsyncFunction 对象。
请注意,AsyncFunction不是一个全局对象。可以通过运行以下代码获得。

Object.getPrototypeOf(async function(){}).constructor

ECMAScript 2015 中引入的 JavaScript 类主要是 JavaScript 现有的基于原型的继承的语法糖。
类语法不是向JavaScript引入一个新的面向对象的继承模型。JavaScript类提供了一个更简单和更清晰的语法来创建对象并处理继承。

类实际上是个“特殊的函数”,就像你可以函数表达式和函数声明一样,类语法有两个组成部分:类表达式和类声明。

@xgqfrms-GitHub
Copy link
Author

xgqfrms-GitHub commented May 16, 2017

Object.getPrototypeOf()

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

Object.getPrototypeOf() 方法返回指定对象的原型(即, 内部[[Prototype]]属性的值)。

object 要返回其原型的对象。
给定的对象的原型。如果没有继承的属性,则返回 null 。

在 ES5 中,如果参数不是一个对象类型,将抛出一个 TypeError 异常。在 ES6 中,参数被强制转换为Object。
mdn-object getprototypeof

Object.getPrototypeOf(window);

// Window {TEMPORARY: 0, PERSISTENT: 1, Symbol(Symbol.toStringTag): "Window", constructor: function}


let proto = {};

proto.name = "xgqfrms";

let obj = Object.create(proto);

Object.getPrototypeOf(obj) === proto;
 // true

Object.prototype.isPrototypeOf()

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

isPrototypeOf() 方法用于测试一个对象是否存在于另一个对象的原型链上。

Note: isPrototypeOf() 与 instanceof 运算符不同。
在表达式 "object instanceof AFunction"中,对象原型链是针对 AFunction.prototype 进行检查的,而不是针对 AFunction 本身。

handler.getPrototypeOf()

https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Proxy/handler/getPrototypeOf

handler.getPrototypeOf() 是一个代理方法,当读取代理对象的原型时,该方法就会被调用。

Object.setPrototypeOf()

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

Object.setPrototypeOf() 方法设置一个指定的对象的原型 ( 即, 内部[[Prototype]]属性)到另一个对象或 null。

Object.prototype.__proto__

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

Object.prototype 的 proto 属性是一个访问器属性(一个getter函数和一个setter函数), 暴露了通过它访问的对象的内部[[Prototype]] (一个对象或 null)。

Object.setPrototypeOf = Object.setPrototypeOf || function (obj, proto) {
  obj.__proto__ = proto;
  return obj; 
}

handler.get()

https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Proxy/handler/get

handler.get() 方法用于拦截对象的读取属性操作。

Object.getPrototypeOf()

Object.getPrototypeOf()

@xgqfrms-GitHub
Copy link
Author

@xgqfrms-GitHub
Copy link
Author

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