GithubHelp home page GithubHelp logo

huang-x-h / as3-promise Goto Github PK

View Code? Open in Web Editor NEW
4.0 2.0 0.0 2.3 MB

参照es6-promise修改成as3版本,实现在flex支持promise模式写法

Home Page: http://huang-x-h.github.io/as3-promise/

License: MIT License

ActionScript 100.00%

as3-promise's Introduction

as3-promise

参照es6-promise修改成as3版本as3-promise,实现在flex支持promise模式写法。

基本用法

Promise promise = new Promise(function(resolve:Function, reject:Function):void {
  // succeed
  resolve(value);
  // or reject
  reject(error);
});

promise.then(function(value:*):void {
  // success
}, function(value:*):void {
  // failure
});

Promise.then示例

//根据给定的url获取相应内容信息
private function get(url:String):Promise {
	return new Promise(function(resolve:Function, reject:Function):void {
		var request:URLRequest = new URLRequest(url);
		var loader:URLLoader = new URLLoader(request);
		loader.addEventListener(Event.COMPLETE, function(e:Event):void {
			resolve(e.target.data);
		});
		loader.addEventListener(IOErrorEvent.IO_ERROR, function(e:IOErrorEvent):void {
			reject(e);
		});
	});
}

get("http://huang-x-h.github.io/as3-promise/menu.json").then(function(result:*):void {
	trace(result);
});

Chaining链接

then方法是可以链接套使用的,如果在then方法第一个处理函数返回内容时,会传递给下一个then处理

get("menu.json").then(function(data:*):void {
  return JSON.parse(data);
}).then(function(json:*):void {
  // proceed
});

错误处理

提供error方法,等同于then方法的第二个参数函数处理

get("menu.json").then(function(result:*):void {
  //proceed;
}).error(function(reason:*):void {
  // failure
});

等价于

get("menu.json").then(function(result:*):void {
  //proceed;
},function(reason:*):void {
  // failure
});

Promise数组处理

提供Promise.all方法,等数组里所有的promise对象成功处理完成,才进行成功处理,当其中任何一个失败,直接进行错误处理。示例

var promise1:Promise = get('directory.json');
var promise2:Promise = get('menu.json');
var promise3:Promise = get('portal.json');

// 当三个json文件都获取完毕时,在进行赋值
Promise.all([promise1, promise2, promise3]).then(function(result:Array):void {
	gridDirectory.dataProvider = new ArrayList(JSON.parse(result[0]) as Array);
	gridMenu.dataProvider = new ArrayList(JSON.parse(result[1]) as Array);
	gridPortal.dataProvider = new ArrayList(JSON.parse(result[2]) as Array);
}).error(function(reason:*):void {
	Alert.show(reason);
});

as3-promise's People

Contributors

f111fei avatar huang-x-h avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.