我编写的代码看起来像:
function getStuffDone(param) { | function getStuffDone(param) {
var d = Q.defer(); /* or $q.defer */ | return new Promise(function(resolve, reject) {
// or = new $.Deferred() etc. | // using a promise constructor
myPromiseFn(param+1) | myPromiseFn(param+1)
.then(function(val) { /* or .done */ | .then(function(val) {
d.resolve(val); | resolve(val);
}).catch(function(err) { /* .fail */ | }).catch(function(err) {
d.reject(err); | reject(err);
}); | });
return d.promise; /* or promise() */ | });
} | }
Run Code Online (Sandbox Code Playgroud)
有人告诉我这个被称为" 延迟反模式 "或" Promise构造函数反模式 ",这个代码有什么不好,为什么这被称为 …
目前,我正在尝试async/await在类构造函数中使用.这样我就可以获得e-mail我正在研究的Electron项目的自定义标签.
customElements.define('e-mail', class extends HTMLElement {
async constructor() {
super()
let uid = this.getAttribute('data-uid')
let message = await grabUID(uid)
const shadowRoot = this.attachShadow({mode: 'open'})
shadowRoot.innerHTML = `
<div id="email">A random email message has appeared. ${message}</div>
`
}
})
Run Code Online (Sandbox Code Playgroud)
但是,目前该项目不起作用,出现以下错误:
Class constructor may not be an async method
Run Code Online (Sandbox Code Playgroud)
有没有办法绕过这个,以便我可以在其中使用async/await?而不是要求回调或.then()?
作为一种组织模式,ES6类可以为异步代码提供什么.下面是ES7 async/await的示例,ES6类可以在ES7中使用异步方法或构造函数吗?
我可不可以做:
class Foo {
async constructor() {
let res = await getHTML();
this.res = res
}
}
Run Code Online (Sandbox Code Playgroud)
而且,如果不是,构造函数应如何工作呢?
class Foo {
constructor() {
getHTML().then( function (res) {
this.res = res
}
}
}
Run Code Online (Sandbox Code Playgroud)
如果这些模式都不起作用,那么ES6中的构造函数(以及类)是否class支持对对象状态进行操作的任何形式的异步性?或者,它们仅用于纯粹的同步代码库吗?上面的例子在构造函数中,但它们不需要是......将问题再推下一级......
class Foo {
myMethod () {
/* Can I do anything async here */
}
}
Run Code Online (Sandbox Code Playgroud)
或者,用吸气剂......
class Foo {
get myProp() {
/* Is there any case that this is usefully asynchronous */
}
}
Run Code Online (Sandbox Code Playgroud)
我能想到的唯一例子是在同一个方法/构造函数/ getter中并行运行某些东西,但是要在结束之前解决整个问题.我只是感到困惑,因为它似乎完全异步库的所有推动,这只是混淆事情.除了教科书示例,我找不到一个他们有用的应用程序.
我怎样才能最好地处理以下情况?
我有一个构造函数需要一段时间才能完成.
var Element = function Element(name){
this.name = name;
this.nucleus = {};
this.load_nucleus(name); // This might take a second.
}
var oxygen = new Element('oxygen');
console.log(oxygen.nucleus); // Returns {}, because load_nucleus hasn't finished.
Run Code Online (Sandbox Code Playgroud)
我看到三种选择,每种选择都与众不同.
一,向构造函数添加回调.
var Element = function Element(name, fn){
this.name = name;
this.nucleus = {};
this.load_nucleus(name, function(){
fn(); // Now continue.
});
}
Element.prototype.load_nucleus(name, fn){
fs.readFile(name+'.json', function(err, data) {
this.nucleus = JSON.parse(data);
fn();
});
}
var oxygen = new Element('oxygen', function(){
console.log(oxygen.nucleus);
});
Run Code Online (Sandbox Code Playgroud)
二,使用EventEmitter发出'loaded'事件.
var …Run Code Online (Sandbox Code Playgroud) 正如问题所述.我会被允许这样做:
class MyClass {
async constructor(){
return new Promise()
}
}
Run Code Online (Sandbox Code Playgroud) 我的nodejs应用程序中有一个类,代码如下:
var mongoose = require('mongoose');
var Roles = mongoose.model('roles');
var Promise = require("bluebird");
module.exports = Role;
var err = null;
var id;
function Role(name, companyId) {
this.err = err;
this.name = name;
this.companyId = companyId;
this.id = getId(name, companyId);
}
var getId = function (name, companyId) {
return new Promise(function(resolve, reject) {
Roles.findOne({companyId:companyId, name:name}, function(err,result) {
resolve(result._id);
});
});
};
Run Code Online (Sandbox Code Playgroud)
当我在调用类时,id正在等待:
var currentRole = new Role(myRole, comId);
console.log(currentRole);
Run Code Online (Sandbox Code Playgroud)
如何在解决时从类中获取值?
我有一个用作本地数据存储的服务来跨应用程序共享数据。该服务在开始时会进行一些休息调用以初始化一些数据。它看起来像这样:
Injectable()
export class DataStoreService {
leadList: LeadModel[]
optyList: ContactModel[]
constructor(public dataService:DataService){
this.initializeData()
}
initializeData() {
this.dataService.getObjectData('opportunities').subscribe(
(data) => this.optyList = data
)
this.dataService.getObjectData('leads').subscribe(
(data) => this.leadList = data
)
}
}
Run Code Online (Sandbox Code Playgroud)
我有一个组件页面,我在下面做:
ngOnInit() {
for(let lead of this.dataStore.leadList){
if(lead.accepted == false)
this.newLeadsList.push(lead)
}
}
Run Code Online (Sandbox Code Playgroud)
很明显,如果初始化数据没有完成,leadList 可能是未定义的,这个 ngOnInit for 循环也会崩溃。那么,如何在 ngOnInit 中等待 initializeData 完成?
假设我创建或拥有node.js库 lib.js
export class C {
constructor(value, callback) {
callback(false, `Hello ${value}`);
}
task(value, callback) {
callback(false, "returned " + value);
}
}
Run Code Online (Sandbox Code Playgroud)
重要的是,类的构造函数需要接受回调,因为它执行数据库连接和文件I/O. 如果我现在导入并使用库回调式,一切都很好(见c1下文).
我真的想宣传我使用它的库,使对象构造更方便(实际上它是一大堆类和方法).
但是,我无法new在一个承诺安全的地方找到适当的方法.
import Promise from 'bluebird';
import * as lib from './lib';
Promise.promisifyAll(lib);
// old style -- works as expected
const c1 = new lib.C("c1", (e, v) => {
console.log(c1, e, v);
});
// assuming c1 got initialized, .task() also works
c1.task("t1", console.log);
c1.taskAsync("t2").then(() => console.log("also works"));
// But …Run Code Online (Sandbox Code Playgroud) getUser是一个异步函数吗?是否需要更长的时间解决?是否总是返回我的正确的价值someotherclass。
class IdpServer {
constructor() {
this._settings = {
// some identity server settings.
};
this.userManager = new UserManager(this._settings);
this.getUser();
}
async getUser() {
this.user = await this.userManager.getUser();
}
isLoggedIn() {
return this.user != null && !this.user.expired;
}
}
let idpServer = new IdpServer();
export default idpServer;
// another class
// import IdpServer from '...'
class SomeOtherClass {
constructor() {
console.log(IdpServer.isLoggedIn());
}
}
Run Code Online (Sandbox Code Playgroud) 我试图在 JavaScript 中创建一个构造函数,这个构造函数应该是异步的,因为我使用 Phantom JS 模块来抓取数据,所以这就是为什么我必须使用异步函数通过 Phantom JS 和 Node JS 来抓取数据。
下面是我的代码,
const phantom = require('phantom');
async function InitScrap() {
var MAIN_URL = "https://www.google.com/",
//Phantom JS Variables
instance = await phantom.create(),
page = await instance.createPage();
// Load the Basic Page First
this.loadPage = async function() {
console.log("Loading Please wait...");
var status = await page.open(MAIN_URL);
if (status == "success") {
page.render("new.png");
console.log("Site has been loaded");
}
}
}
var s = new InitScrap();
s.loadPage()
// module.exports = InitScrap();Run Code Online (Sandbox Code Playgroud)
但是当我运行这段代码时,它说,
InitScrap() …
javascript ×8
node.js ×5
promise ×4
asynchronous ×3
bluebird ×3
async-await ×2
constructor ×2
ecmascript-6 ×2
es6-promise ×2
angular ×1
es6-class ×1
phantomjs ×1
q ×1
typescript ×1