mirror of
https://codeberg.org/keyoxide/doipjs.git
synced 2024-12-23 06:59:29 -07:00
Improve promise handling
This commit is contained in:
parent
ff93ceee0d
commit
0bc97bd902
2 changed files with 32 additions and 28 deletions
|
@ -27,17 +27,19 @@ module.exports = async (roomId, eventId, opts) => {
|
||||||
|
|
||||||
const url = `https://${opts.instance}/_matrix/client/r0/rooms/${roomId}/event/${eventId}?access_token=${opts.accessToken}`
|
const url = `https://${opts.instance}/_matrix/client/r0/rooms/${roomId}/event/${eventId}?access_token=${opts.accessToken}`
|
||||||
|
|
||||||
const fetchPromise = bentReq(url, null, {
|
const fetchPromise = new Promise((resolve, reject) => {
|
||||||
|
bentReq(url, null, {
|
||||||
Accept: 'application/json',
|
Accept: 'application/json',
|
||||||
})
|
})
|
||||||
.then(async (data) => {
|
.then(async (data) => {
|
||||||
return await data.json()
|
return await data.json()
|
||||||
})
|
})
|
||||||
.then((data) => {
|
.then((data) => {
|
||||||
return data
|
resolve(data)
|
||||||
})
|
})
|
||||||
.catch((error) => {
|
.catch((error) => {
|
||||||
return error
|
reject(error)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
return Promise.race([fetchPromise, timeoutPromise]).then((result) => {
|
return Promise.race([fetchPromise, timeoutPromise]).then((result) => {
|
||||||
|
|
|
@ -25,7 +25,8 @@ module.exports = async (tweetId, opts) => {
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
const fetchPromise = bentReq(
|
const fetchPromise = new Promise((resolve, reject) => {
|
||||||
|
bentReq(
|
||||||
`https://api.twitter.com/1.1/statuses/show.json?id=${tweetId}&tweet_mode=extended`,
|
`https://api.twitter.com/1.1/statuses/show.json?id=${tweetId}&tweet_mode=extended`,
|
||||||
null,
|
null,
|
||||||
{
|
{
|
||||||
|
@ -37,10 +38,11 @@ module.exports = async (tweetId, opts) => {
|
||||||
return await data.json()
|
return await data.json()
|
||||||
})
|
})
|
||||||
.then((data) => {
|
.then((data) => {
|
||||||
return data.full_text
|
resolve(data.full_text)
|
||||||
})
|
})
|
||||||
.catch((error) => {
|
.catch((error) => {
|
||||||
return error
|
reject(error)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
return Promise.race([fetchPromise, timeoutPromise]).then((result) => {
|
return Promise.race([fetchPromise, timeoutPromise]).then((result) => {
|
||||||
|
|
Loading…
Reference in a new issue