小编Cal*_*ein的帖子

ES2015相当于$ .Deferred()

我正在使用Babel进行项目,我遇到了一个非常基本的问题.我已经习惯了jQuery的Deferred对象而且我很难找到它的ES2015等价物,这是我基本上想要的:

// file1.js
let dfd = new Promise()

function functionCalledAtSomePoint(thing) {
    dfd.resolve(thing)
}

export default { dfd }


// file2.js
import { dfd } from './file1'

dfd.then((thing) => {
    console.log('Yay thing:', thing)
})
Run Code Online (Sandbox Code Playgroud)

什么应该是正确的方法来纠正这个简单的延期?

用royhowie的回答编辑:

// file1.js
let thing
function getThing(_thing) {
    return new Promise((resolve) => {
        if (el) {
            thing = new Thing(el)
        }
        resolve(thing)
    })
}

function functionCalledAtSomePoint(el) {
    getThing(el)
}

export default { getThing }


// file2.js
import { getThing } from './file1'

getThing.then((thing) => {
    console.log('Yay …
Run Code Online (Sandbox Code Playgroud)

javascript jquery promise ecmascript-6 es6-promise

4
推荐指数
2
解决办法
1425
查看次数

选择在Yeoman中生成命令的位置

我正在构建一个生成器,我最后需要npm install在我的应用程序中的某个目录中,我尝试过以下方法:

this.spawnCommandSync('cd', [this.destinationRoot() + '/my/folder'])
this.spawnCommandSync('npm', ['install'])
Run Code Online (Sandbox Code Playgroud)

但它不起作用.

并且npm install不能在不同的目录中调用,也不能在任何yeoman安装mixins中调用.

cd spawn yeoman yeoman-generator

4
推荐指数
1
解决办法
1371
查看次数

在虚拟getter中添加参数

我想做的是这样的:

Schema
.virtual('getSomething')
.get(function(what) {
    if (!what) {
        return this.somethingElse
    } else {
        return this.something[what]
    }
})
Run Code Online (Sandbox Code Playgroud)

问题是我们不能在虚拟getter中传递参数,如何在不重复代码的情况下实现类似的东西呢?

virtual mongoose node.js

3
推荐指数
1
解决办法
2249
查看次数