4
4
4sadly2020-05-06 22:23:00
JavaScript
4sadly, 2020-05-06 22:23:00

Why does return fire before the rest of the code?

async function upload_photo(attachment){
  let new_photo = fs.createWriteStream('tmp.jpg')
  let new_photo_downloaded = await axios.get(attachment, {
    method: 'GET',
    responseType: 'stream'
  })
  new_photo_downloaded.data.pipe(new_photo)

  await new Promise((resolve, reject) => {
    new_photo.on('finish', resolve)
    new_photo.on('error', reject)
  })

  let options = await api('photos.getUploadServer', {
    album_id: config.album_id,
    group_id: config.group_id,
    access_token: config.access_token
  })
  let form = new FormData()
  form.append('photo', await fs.createReadStream('tmp.jpg'), 'tmp.jpg')
  const headers = form.getHeaders()
  let response = await axios.post(options.response.upload_url, form, {headers: headers})
  let photo_data = response.data
  let photo_upload = await api('photos.save',
  {
    album_id: config.album_id,
    group_id: config.group_id,
    photos_list: photo_data.photos_list,
    server: photo_data.server,
    hash: photo_data.hash,
    access_token: config.access_token
  }
  )
  let photo = 'photo' + photo_upload.response[0].owner_id + '_' + photo_upload.response[0].id
  return photo
}

Why does return fire before the rest of the code?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Karpion, 2020-05-06
@Karpion

The word "faster" should be accompanied by a comma, the word "than" and then the entity that works more slowly. Without this, the question is completely incomprehensible.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question