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