Fetching the asset ID in JS API batching requests

The problem is that the createFileAsset function returns an object that isn’t useful in conjunction with the batchRequest results keyword. It requires you know the property name to access the value, and the property name is the newly created, unknown, asset id.

eg createFileAsset
{ “1234” : “New File Asset (#1234) ‘filename.ext’ of type_code ‘File’ created successfully” }

whereas createAsset returns
{ “name” : “Test”, “id” : 1234, “link_id” : 5678 }
results_0_id => 1234

and no unfortunately you can’t just use createAsset to create file assets.

JohnGill offers a solution here

Otherwise you can abandon batchRequest and chain callbacks. This is more effort (requires more manual error checking) but allows you to extract the created asset id.
eg

js_api.createFileAsset({
            "parentID":1234,
            "type_code":"file",
            "friendly_name":"filename.ext",
            "dataCallback": acquireFileAssetLocks
})

...
acquireFileAssetLocks: function(o) {
    js_api.acquireLock({
        "asset_id":Object.keys(o)[0],
        "dataCallback": uploadFile
    });
}
...

(This could probably be fixed by Squiz while mostly retaining backwards compatibility by amending the createFileAsset return object with additional properties:)
{ “1234” : “New File Asset (#1234) ‘filename.ext’ of type_code ‘File’ created successfully”, “name” : “filename.ext”, “id” : 1234, “link_id” : 5678 }