Skip to content
This repository was archived by the owner on Feb 12, 2024. It is now read-only.
This repository was archived by the owner on Feb 12, 2024. It is now read-only.

Could we use vanilla JavaScript in the 101 example? #1491

@davidgilbertson

Description

@davidgilbertson

Type: Enhancement

Severity: Very Low

Description: Use vanilla JavaScript in the 101 example

Would you accept a PR that changes the 101 example to not require a third party npm package (the async package)?

For me, the async package syntax was just another thing I needed to wrap my mind around, when native async/await is so much prettier.

Existing

series([
  (cb) => node.on('ready', cb),
  (cb) => node.version((err, version) => {
    if (err) { return cb(err) }
    console.log('Version:', version.version)
    cb()
  }),
  (cb) => node.files.add({
    path: 'hello.txt',
    content: Buffer.from('Hello World 101')
  }, (err, filesAdded) => {
    if (err) { return cb(err) }

    console.log('\nAdded file:', filesAdded[0].path, filesAdded[0].hash)
    fileMultihash = filesAdded[0].hash
    cb()
  }),
  (cb) => node.files.cat(fileMultihash, (err, data) => {
    if (err) { return cb(err) }

    console.log('\nFile content:')
    process.stdout.write(data)
  })
])

Proposed

node.on('ready', async () => {
  const version = await node.version();

  console.log('Version:', version.version);

  const filesAdded = await node.files.add({
    path: 'hello.txt',
    content: Buffer.from('Hello World 101'),
  });

  console.log('Added file:', filesAdded[0].path, filesAdded[0].hash);

  const fileBuffer = await node.files.cat(filesAdded[0].hash);

  console.log('Added file contents:', fileBuffer.toString());
});

(I think it's OK to leave error handling out of a 101)

Or was this left out because the required Node version is 6 but async/await wasn't supported until 7?

Metadata

Metadata

Assignees

No one assigned

    Labels

    exampleexp/noviceSomeone with a little familiarity can pick up

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions