私はプロジェクトに 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)
})
この単純な遅延を正しくする正しい方法は何ですか?
ロイハウの答えで編集:
// 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 thing:', thing)
})