gracekrcx / weekly-notes

4 stars 0 forks source link

(筆記)async / await 使用 try-catch #114

Open gracekrcx opened 2 years ago

gracekrcx commented 2 years ago

起因

Doc : doc

Usage 範例:

import * as Keychain from 'react-native-keychain';

async () => {
  const username = 'zuck';
  const password = 'poniesRgr8';

  // Store the credentials
  await Keychain.setGenericPassword(username, password);

  try {
    // Retrieve the credentials
    const credentials = await Keychain.getGenericPassword();
    if (credentials) {
      console.log(
        'Credentials successfully loaded for user ' + credentials.username
      );
    } else {
      console.log('No credentials stored');
    }
  } catch (error) {
    console.log("Keychain couldn't be accessed!", error);
  }
  await Keychain.resetGenericPassword();
};

參考文章: JavaScript 之旅 (7):Async Functions & await (2)