简体   繁体   English

由异步创建未定义无法解决?

[英]undefined created by async not resolving?

I currently have the following scenario. 我目前有以下情况。 I cant' really figure out why this is happening it might be to do with it not resolving in time or something else. 我真的无法弄清楚为什么会发生这种情况,可能是因为它没有及时解决问题或其他原因。

According to documentation on Expo. 根据世博会的文件。 The following method would return an object. 以下方法将返回一个对象。

MediaLibrary.getAlbumAsync('pleaseDontDownVote');

MediaLibrary is provides a users camera roll and the method getAlbumAsync is getting the album named 'pleaseDontDownVote'. MediaLibrary为用户提供了一个相机胶卷,方法getAlbumAsync正在获取名为“ pleaseDontDownVote”的相册。 I've confirmed MediaLibrary works, as it was used to create the album and also I've confirmed the album 'pleaseDontDownVote' appears on the device. 我已经确认MediaLibrary可以正常工作,因为它是用来创建相册的,并且我已经确认相册“ pleaseDontDownVote”出现在设备上。

I then have another method which is: 然后,我有另一种方法是:

savePictures = () => {
      let getCameraRoll = this.orderPhotoAlbum();
      getCameraRoll.then(cameraRollItem => console.log(cameraRollItem))
}

Here this is what I 'think' I'm doing, my method savePictures is executing this.orderPhotoAlbum in getCameraRoll then I'm waiting for it to execute so that I can proceed. 这就是我在想的事情,我的方法savePictures在getCameraRoll中执行this.orderPhotoAlbum,然后等待其执行,以便继续进行。

async orderPhotoAlbum() {
  await MediaLibrary.getAlbumAsync('pleaseDontDownVote');
}

The MediaLibrary method is wrapped in an async method so it would wait for the response. MediaLibrary方法包装在异步方法中,因此它将等待响应。 I'm thinking orderPhotoAlbum() would wait to get the response and then that would be passed to my "then". 我在想orderPhotoAlbum()将等待获取响应,然后将其传递给我的“然后”。 The console.log in getCameraRoll.then(cameraRollItem => console.log(cameraRollItem)) says undefined. getCameraRoll.then(cameraRollItem => console.log(cameraRollItem))表示未定义。 Is this a problem with the way I'm managing the async/await? 这是我管理异步/等待方式的问题吗?

NOTE: I tagged this js because I don't think it has to do with Expo all that much as it does with the async/await. 注意:我标记了这个js,因为我认为它与Expo无关,与异步/等待无关。 I could be wrong thou. 我可能是错的。

orderPhotoAlbum isn't returning anything, hence the undefined . orderPhotoAlbum不返回任何内容,因此undefined

async orderPhotoAlbum() {
  return await MediaLibrary.getAlbumAsync('pleaseDontDownVote');
}

Edit: As Jake mentioned, at this point the await and async are redundant. 编辑:正如杰克提到,在这一点上, awaitasync是多余的。 I kept them because it makes it really clear that the function returns a promise. 之所以保留它们,是因为它使函数很清楚地返回了一个Promise。 It's just a matter of personal preference though. 不过,这只是个人喜好问题。

orderPhotoAlbum() as it is currently written does not return the result of MediaLibrary.getAlbumAsync('pleaseDontDownVote'); orderPhotoAlbum()当前编写的orderPhotoAlbum()不会返回MediaLibrary.getAlbumAsync('pleaseDontDownVote');的结果MediaLibrary.getAlbumAsync('pleaseDontDownVote'); , since await does not return by default like it seems you are expecting it to. ,因为默认情况下await不会像您期望的那样返回。 Try changing it to: 尝试将其更改为:

function orderPhotoAlbum() {
  return MediaLibrary.getAlbumAsync('pleaseDontDownVote');
}

Note that is no longer an async function, but it still returns a Promise so .then will work on it. 请注意,它不再是异步函数,但仍返回Promise因此.then可以在其上使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM