0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Promiseシンプル構文

Posted at

非同期処理で使われるPromise関数にはnew PromisePromise.resolveで行う方法があります。
良く紹介されている方法はnew Promiseで行う方法が多いですが、シンプルに書きたいときはPromise.resolveで行うとよいです。

###よくある構文

var result = new Promise(function(resolve) {
    resolve('Hello1')
})

result
.then(function(data){
    console.log(data)
    return new Promise(function(resolve){
        resolve('Hello2')
    })
})
.then(function(data){
    console.log(data)
    return new Promise(function(resolve){
        resolve('Hello3')
    })
}).then(function(data){
    console.log(data)
})

###シンプルな構文

Promise.resolve('Hello1')
.then(function(data){
    console.log(data)
    return Promise.resolve('Hello2')
})
.then(function(data){
    console.log(data)
    return Promise.resolve('Hello3')
})
.then(function(data){
    console.log(data)
})
0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?