我正在使用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) 我正在构建一个生成器,我最后需要npm install在我的应用程序中的某个目录中,我尝试过以下方法:
this.spawnCommandSync('cd', [this.destinationRoot() + '/my/folder'])
this.spawnCommandSync('npm', ['install'])
Run Code Online (Sandbox Code Playgroud)
但它不起作用.
并且npm install不能在不同的目录中调用,也不能在任何yeoman安装mixins中调用.
我想做的是这样的:
Schema
.virtual('getSomething')
.get(function(what) {
if (!what) {
return this.somethingElse
} else {
return this.something[what]
}
})
Run Code Online (Sandbox Code Playgroud)
问题是我们不能在虚拟getter中传递参数,如何在不重复代码的情况下实现类似的东西呢?
cd ×1
ecmascript-6 ×1
es6-promise ×1
javascript ×1
jquery ×1
mongoose ×1
node.js ×1
promise ×1
spawn ×1
virtual ×1
yeoman ×1