javascript singleton with promise
javascript singleton with promise
我想知道我是否有这样的单例对象
var A = require("js/A");
var singleton = {
obj : null,
current: function() {
if(!this.obj){
this.fetch();
}
return this.obj;
},
fetch: function() {
A.fetch().then(function(result){
this.obj = result;
});
}
}
那么它是否等待我们return this.obj
时提取回来?
简短的回答是否定的。 获取是异步发生的,因此外部函数将在执行传递到A.fetch()
的回调函数之前返回。
这就是异步编程的本质,你需要熟悉promises和/或jQuery的延迟对象(这是Promises/A规范的部分实现)来处理这种情况。
这是对承诺的一个很好的介绍,这是另一个
相关文章:
- 使用promise和mongoose对文档进行排序
- Fighting with FRP
- issue with FB.Event.subscribe
- 测试Angular Service解决错误回调中的promise
- geolocation-marker.js conflict with markerclusterer.js
- Angular 2.0 with JavaScript or TypeScript?
- javascript singleton with promise
- Promise/A+ with chain then() 回调用例
- 如何在 Node JS FS 模块中使用 Typescript Async/ await with promise
- Chained Promise with Express:在最后一步中未收到任何数据
- Promise.all behavior with RxJS Observables?
- Jasmine Testing with Angular Promise 无法使用 TypeScript 解析
- NodeJS:在循环中使用 Promise with Q
- Javascript Q Promise Sequence Order with Arguments
- angularjs ng-show with promise expression
- Coffeescript Promise with Jquery Post
- AngularJS ui router $stateChangeStart with promise无限循环
- undefined with promise async / await
- ' fetch ', work with mode ' no-cors '不会调用promise.然后回调
- $resource angular returning undefined with $promise