Dive into secure and efficient coding practices with our curated list of the top 10 examples showcasing 'await-to-js' in functional components in JavaScript. Our advanced machine learning engine meticulously scans each line of code, cross-referencing millions of open source libraries to ensure your implementation is not just functional, but also robust and secure. Elevate your React applications to new heights by mastering the art of handling side effects, API calls, and asynchronous operations with confidence and precision.
async loadFromJson(json) {
const { addNode, addLink, clearDatabase, updateNode } = this.props;
this.setPercent(10); // Loading started!
clearDatabase();
const [error, response] = await to(loadPorkFile(json, this.setPercent));
if (error) {
console.log(
"Error reading Porcupine Config file! Either data is missing or format is incorrect"
);
return;
} else {
this.setPercent(-1);
}
const { nodes, links } = response;
try {
let i = 0;
nodes.forEach(node => {
addNode(node);
updateNode(node.id);
this.setPercent(50 + (30 * i++) / nodes.length);
});
}
const [err, down] = await to(downloadImage(url));
handleError('DOWNLOADING', 'the demo image failed', err);
// Spinner Step.
spinner.succeed(`${green(`DOWNLOADED`)} the image in directory: ${dim(UPLOAD_DIR)}`);
spinner.succeed(`ALL DONE! \n`);
} else {
if (!url) {
const spinner = ora({ text: '' });
spinner.warn(`${yellow(` URL`)} cannot be empty. \n${dim(`You forgot the first parameter.`)}\n`);
process.exit(0);
}
const [err, down] = await to(downloadImage(url));
handleError('DOWNLOADING', 'the demo image failed', err);
}
})();
};
async checkNodeStatus() {
const { errorToastDisplayed, history, liskAPIClient } = this.props;
// istanbul ignore else
if (liskAPIClient) {
const [error, response] = await to(liskAPIClient.node.getConstants());
if (response) history.push(routes.dashboard.path);
if (error) errorToastDisplayed({ label: `Unable to connect to the node, Error: ${error.message}` });
}
}
if (block.type === 'custom') {
[err, allFiles] = await to(
extractCustomBlock(
block,
path.join(componentsDir, blockName),
progressFunc
)
);
if (err) {
throw new Error(`解压自定义区块${blockName}出错,请重试`);
}
return allFiles;
}
// 通过 npm 源获取区块
[err, tarballURL] = await to(
materialUtils.getTarballURLBySource(block.source, projectVersion)
);
if (err) {
err.message = `请求区块 ${tarballURL} 包失败`;
throw new Error(err);
}
[err, allFiles] = await to(
retryExtractBlock(
path.join(componentsDir, blockName),
tarballURL,
clientPath,
progressFunc
)
);
blocks.map(async (block) => {
return await downloadBlockToPage(
{ clientPath, clientSrcPath, pageName, block },
progressFunc
);
})
)
);
if (err) {
throw err;
}
const pkg = getPackageByPath(clientPath);
const projectVersion = getProjectVersion(pkg);
[err, depList] = await to(
Promise.all(
filesList.map(async (_, idx) => {
const block = blocks[idx];
// 根据项目版本下载依赖
// 兼容旧版物料源
if (block.npm && block.version && block.type !== 'custom') {
return getDependenciesFromNpm({
npm: block.npm,
version: block.version,
});
} if (
block.source
&& block.source.type === 'npm'
&& block.type !== 'custom'
) {
let version = block.source.version;
throw new Error(`解压自定义区块${blockName}出错,请重试`);
}
return allFiles;
}
// 通过 npm 源获取区块
[err, tarballURL] = await to(
materialUtils.getTarballURLBySource(block.source, projectVersion)
);
if (err) {
err.message = `请求区块 ${tarballURL} 包失败`;
throw new Error(err);
}
[err, allFiles] = await to(
retryExtractBlock(
path.join(componentsDir, blockName),
tarballURL,
clientPath,
progressFunc
)
);
if (err) {
if (err.code === 'ETIMEDOUT' || err.code === 'ESOCKETTIMEDOUT') {
throw new Error(`解压区块${blockName}超时,请重试`);
}
throw new Error(`解压区块${blockName}出错, 请重试`);
}
return allFiles;
action: 'download-block',
data: {
name: block.name,
},
});
// 根据项目版本下载
const pkg = getPackageByPath(clientPath);
const projectVersion = getProjectVersion(pkg);
const blockName = block.alias || upperCamelCase(block.name) || block.className;
let err, tarballURL, allFiles;
// 通过 iceland 自定义的区块
if (block.type === 'custom') {
[err, allFiles] = await to(
extractCustomBlock(
block,
path.join(componentsDir, blockName),
progressFunc
)
);
if (err) {
throw new Error(`解压自定义区块${blockName}出错,请重试`);
}
return allFiles;
}
// 通过 npm 源获取区块
[err, tarballURL] = await to(
materialUtils.getTarballURLBySource(block.source, projectVersion)
);
return new Promise(async (resolve, reject) => {
let version = source.version;
// 注意!!! 由于接口设计问题,version-0.x 字段实质指向1.x版本!
if (projectVersion === '1.x') {
// 兼容没有'version-0.x'字段的情况
version = source['version-0.x'] || source.version;
}
const registry = typeof source.npm === 'string' && source.npm.startsWith('@icedesign')
? 'https://registry.npm.taobao.org'
: env.npm_config_registry || source.registry;
const [err, pkgData] = await to(
npmRequest({
name: source.npm,
version,
registry,
})
);
if (err) {
reject(err);
} else {
resolve(pkgData.dist.tarball);
}
});
};
module.exports = async function createProject(
{ destDir, scaffold, projectName, interpreter, progressFunc },
afterCreateRequest
) {
let err, tarballURL, extractedFiles;
[err, tarballURL] = await to(utils.getTarballURLBySource(scaffold.source));
if (err) {
err.message = `请求模板 ${tarballURL} 包失败`;
throw err;
}
[err, extractedFiles] = await to(
utils.extractTarball(
tarballURL,
destDir,
scaffold.source,
progressFunc,
afterCreateRequest
)
);
if (err) {
throw err;
}
// 写 package.json 文件
const pkgJSONPath = path.join(destDir, 'package.json');
let pkgJSON = fs.readFileSync(pkgJSONPath);
(async function() {
const [error1, data] = await to(fs.readJson('./package.json'));
if (error1) return console.error(error1);
data.dependencies = _.omit(data.dependencies, [
'expo',
'react',
'react-native',
]);
data.private = false;
const [error2] = await to(
fs.writeJson('./dist/package.json', data, { spaces: 2 })
);
if (error2) return console.error(error2);
})();