Promise 示例


【Promise example】

基于 Promise 的操作会返回一个 Promise,当异步操作完成时,该 Promise 会被兑现。

【Promise-based operations return a promise that is fulfilled when the asynchronous operation is complete.】

import { unlink } from 'node:fs/promises';

try {
  await unlink('/tmp/hello');
  console.log('successfully deleted /tmp/hello');
} catch (error) {
  console.error('there was an error:', error.message);
}const { unlink } = require('node:fs/promises');

(async function(path) {
  try {
    await unlink(path);
    console.log(`successfully deleted ${path}`);
  } catch (error) {
    console.error('there was an error:', error.message);
  }
})('/tmp/hello');