[TOC]
  • callback问题releasing Zalgo参考资料

callback问题

js编程遇到的最大问题就是单线程异步问题,这里面涉及最多的肯定就是callback了,不能处理好callback问题,常常会出现大量的嵌套情况,就是著名的callback hell了。

ES6中会引入一个新的规范,叫做Promise。这可以规范我们使用异步的情况。

releasing Zalgo

What it means is a function that accepts a callback and sometimes returns it right away, and some other times it returns it after some delay, in the future.

就是我们的代码之中的callback,可能sync,也可能async触发,比如:

function register(options, callback) {    var first_name = (options['first_name'] || '').trim();    var last_name = (options['last_name'] || '').trim();    var errors = [];    if (!first_name) {        errors.push(['first_name', 'Please enter a valid name']);    }    if (!last_name) {        errors.push(['last_name', 'Please enter a valid name']);    }    if (errors.length) {        return callback(null, errors);    }    var params = {        'user': {            'email': options['email'],            'first_name': first_name,            'last_name': last_name,            'new_password': options['new_password'],            'new_password_confirmation': options['new_password_confirmation'],            'terms': '1'        },        'vrid': options['vrid'],        'merge_history': options['merge_history'] || 'true'    };    requestWithSignature('post', '/api/v2/users', params, callback);}

而最好的做法,是保证callback全是sync或者async, 那么将上面的修改为:

if (errors.length) {  process.nextTick(function() {    callback(null, errors);  });  return;}

就可以避免releasing Zalgo。

参考资料

  • Callback 在大型编程时的一般性问题
  • Designing APIs for Asynchrony
  • Don’t release Zalgo!

原文: https://leohxj.gitbooks.io/front-end-database/content/javascript-advance/callback.html